Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔴 Advanced  ·  Lesson 62

Modern C++ Features

"Modern C++" का मतलब क्या है?

C++ 1998 में standardise हुई। तेरह साल तक इसमें बहुत कम बदलाव हुआ। फिर C++11 इतने सुधार लेकर आया कि लोग इसे "modern C++" कहने लगे। अब हर तीन साल में नया standard आता है:

Standardवर्षमुख्य features
C++112011auto, lambdas, smart pointers, move semantics, nullptr, range-based for, constexpr, threads
C++142014Generic lambdas, make_unique, return type deduction, binary literals
C++172017Structured bindings, std::optional, initialiser वाला if, string_view, inline variables
C++202020Concepts, ranges, std::format, modules, coroutines, <=> operator
C++232023std::print, std::expected, और ranges

यह lesson रोज़ काम आने वाले features दिखाता है, हर एक *पुराना तरीका* vs *modern तरीका* के रूप में। उदाहरण g++ -std=c++17 (या नए) से compile करें।

auto — type compiler को निकालने दें

C++
std::map<std::string, std::vector<int>> marks;

// पुराना
std::map<std::string, std::vector<int>>::iterator it = marks.begin();
// modern
auto it2 = marks.begin();

Type फिर भी compile time पर तय होता है। Type साफ़ या बहुत लंबा हो तो auto लें; पढ़ने वाले की मदद हो तो type लिखें।

Range-based for loop

C++
std::vector<int> v = {78, 91, 66};
// पुराना
for (size_t i = 0; i < v.size(); i++) std::cout << v[i] << " ";
// modern
for (int m : v) std::cout << m << " ";          // copy
for (int& m : v) m += 5;                        // वहीं बदलो
for (const auto& m : v) std::cout << m << " ";  // बड़े objects बिना copy पढ़ो

NULL की जगह nullptr

C++
int* p = nullptr;      // modern: अपने type वाली असली null-pointer value
// int* q = NULL;      // पुराना: असल में सिर्फ 0, overload चुनने में उलझन

Uniform (brace) initialisation

C++
int count{};                          // 0
std::vector<int> v{1, 2, 3};
struct Point { int x, y; };
Point p{3, 4};
int narrow{3.7};                      // error: braces narrowing conversion रोकते हैं

Smart pointers

C++
#include <memory>
// पुराना
Student* s = new Student("Aman");
delete s;                             // भूलना आसान
// modern
auto s2 = std::make_unique<Student>("Aman");   // अपने-आप delete
auto s3 = std::make_shared<Student>("Priya");  // साझा ownership, reference counted

देखें smart pointers।

Lambdas

C++
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });

देखें lambda expressions।

Move semantics

दस लाख elements वाले vector की copy महँगी है। Move बस उसका अंदरूनी buffer सौंप देता है।

C++
#include <iostream>
#include <vector>
#include <utility>

int main() {
    std::vector<int> a(1000000, 7);
    std::vector<int> b = std::move(a);   // कोई element copy नहीं; a खाली रह जाता है
    std::cout << "a: " << a.size() << ", b: " << b.size() << "\n";
    return 0;
}
a: 0, b: 1000000

Function से local vector लौटाने पर वह अपने-आप move होता है (या copy पूरी तरह हट जाती है), इसलिए return result; सस्ता है।

constexpr — compile time पर गणना

C++
constexpr int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); }
constexpr int f5 = factorial(5);      // 120, compiler ने निकाला
int table[factorial(4)];              // 24 ints का array

C++17 के पसंदीदा features

C++
#include <iostream>
#include <map>
#include <optional>
#include <string>

std::optional<int> findMarks(const std::map<std::string, int>& m, const std::string& name) {
    auto it = m.find(name);
    if (it == m.end()) return std::nullopt;    // "कोई value नहीं"
    return it->second;
}

int main() {
    std::map<std::string, int> marks{{"Aman", 78}, {"Priya", 91}};

    for (const auto& [name, score] : marks)             // structured bindings
        std::cout << name << " = " << score << "\n";

    if (auto r = findMarks(marks, "Ravi"); r.has_value())   // initialiser वाला if
        std::cout << "Ravi = " << *r << "\n";
    else
        std::cout << "Ravi not found\n";

    std::cout << "Priya = " << findMarks(marks, "Priya").value_or(0) << "\n";
    return 0;
}
Aman = 78 Priya = 91 Ravi not found Priya = 91
  • Structured bindings pairs, tuples और structs को खोलते हैं: auto [x, y] = point;
  • **std::optional<T>** में या तो T होता है या कुछ नहीं — "नहीं मिला" के लिए -1 लौटाने से ज़्यादा साफ़।
  • **if (init; condition)** helper variable को if के अंदर ही रखता है।
  • **std::string_view** string का सस्ता, read-only view है (copy नहीं)।

C++20 की झलक

  • **std::format** — Python जैसी formatting: std::format("{} scored {}", name, marks)।
  • Ranges — begin() और end() देने की जगह std::ranges::sort(v);।
  • Concepts — templates पर शर्त: template<std::integral T> T gcd(T a, T b);
  • Three-way comparison <=> — एक operator से <, <=, >, >= बन जाते हैं।
  • **std::erase_if(v, pred)** — मिलते elements एक call में हटाएँ।

GCC 11+, Clang 14+ और नए MSVC में C++20 का अच्छा support है; -std=c++20 से compile करें।

पुराना style vs modern style

कामपुराना C++Modern C++
Dynamic arrayint* a = new int[n]; ... delete[] a;std::vector<int> a(n);
Owning pointerT* p = new T; ... delete p;auto p = std::make_unique<T>();
Null pointerNULL या 0nullptr
Container पर loopindex या iterator looprange-based for
Sort के लिए comparatorअलग नाम वाला functionlambda
Constants#define MAX 100constexpr int MAX = 100;
"शायद value है"-1 जैसी खास valuestd::optional<int>

आम गलतियाँ

  • Turbo C++ या -std=c++98 इस्तेमाल करना और सोचना कि auto/lambdas क्यों नहीं चलते — IDE में -std=c++17 set करें।
  • Braces के साथ auto: auto x{5}; int है, पर auto y = {5}; std::initializer_list<int> है।
  • std::move के बाद variable को ऐसे इस्तेमाल करना जैसे उसमें पुरानी value हो।
  • const auto& चाहते हुए for (auto m : bigObjects) (copies) लिखना।
🏋️ अभ्यास

अपना कोई पुराना program modern style में दोबारा लिखें: new[]/delete[] की जगह vector, NULL की जगह nullptr, index loops की जगह range-based for, #define की जगह constexpr, और "न मिले तो -1 लौटाओ" वाले function की जगह std::optional।

सारांश

  • Modern C++ = C++11 और उसके बाद; कम से कम -std=c++17 लें।
  • रोज़ के features: auto, range-for, nullptr, brace init, smart pointers, lambdas।
  • Move semantics से बड़े objects भेजना और लौटाना सस्ता होता है।
  • C++17 structured bindings, optional, if-init जोड़ता है; C++20 format, ranges, concepts।
  • Modern style का मतलब कम raw pointers और कम manual delete।

संबंधित: smart pointers, lambda, templates, C++ best practices।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Modern C++ क्या है?
"Modern C++" का मतलब आमतौर पर C++11 और उसके बाद के standards (C++14, C++17, C++20, C++23) है। C++11 बड़ा update था जिसने auto, lambdas, smart pointers, move semantics, nullptr, range-based for और बहुत कुछ जोड़ा, और रोज़ के C++ code लिखने का तरीका बदल दिया।
Student को C++ का कौन-सा version इस्तेमाल करना चाहिए?
कम से कम C++17 लें; मौजूदा GCC, Clang और MSVC इसे पूरी तरह support करते हैं और नए GCC versions में यही default है। अगर compiler या IDE पुराना default इस्तेमाल करे तो -std=c++17 (या -std=c++20) से compile करें। Turbo C++ modern C++ बिल्कुल support नहीं करता।
C++ में auto क्या करता है?
C++11 से auto compiler से कहता है कि variable का type उसकी initial value से निकाल ले, जैसे auto x = 3.5; से x double बनता है, और auto it = v.begin(); से लंबा iterator type नहीं लिखना पड़ता। Type फिर भी compile time पर तय होता है — C++ dynamically typed नहीं बनती।
C++ में move semantics क्या है?
Move semantics (C++11) से एक object अपने resources, जैसे बड़ा buffer, copy करने की जगह दूसरे object को सौंप सकता है। std::move(x) x को movable mark करता है। इससे std::vector और std::string जैसे बड़े objects को लौटाना और भेजना बहुत सस्ता हो जाता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicSorting and Searching in C++ आपकी प्रगति: 62 / 81 (77%) · Lesson 63 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें