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

Standard Template Library (STL)

STL क्या है?

मान लीजिए आपको कितने भी students के marks रखने हैं, उन्हें sort करना है और सबसे ज़्यादा marks खोजने हैं। C में आप अपना dynamic array, अपना sort और अपना search लिखेंगे — और तीनों को debug करेंगे। Standard Template Library (STL) यह सब तैयार, tested और तेज़ रूप में देती है:

C++
std::vector<int> marks = {78, 91, 66, 84};
std::sort(marks.begin(), marks.end());

STL data structures और algorithms के लिए templates — किसी भी data type के साथ चलने वाला generic code — का संग्रह है। इसे Alexander Stepanov ने design किया और 1998 में यह standard C++ का हिस्सा बनी। यह lesson पहली झलक है; गहराई के लिए देखें C++ STL guide।

चार components

Componentक्या करता हैउदाहरण
ContainersObjects का संग्रह रखते हैंvector, list, deque, set, map, unordered_map
IteratorsElements की ओर इशारा करते हैं और container में चलते हैंbegin(), end(), ++it, *it
AlgorithmsElements की range पर काम करते हैंsort, find, count, reverse, max_element
Function objects / lambdasAlgorithms का व्यवहार बदलते हैंstd::greater<int>(), [](int a, int b){ return a > b; }

मुख्य design विचार: algorithms containers के बारे में नहीं जानते। वे iterators की जोड़ी (range) पर काम करते हैं, इसलिए एक ही sort vector, deque या साधारण array पर चलता है।

पहला उदाहरण: रखना, sort करना, खोजना

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

int main() {
    std::vector<int> marks = {78, 91, 66, 84, 91, 55};

    marks.push_back(73);                                   // container बढ़ता है

    std::sort(marks.begin(), marks.end());                 // algorithm
    std::cout << "Sorted: ";
    for (int m : marks) std::cout << m << " ";
    std::cout << "\n";

    auto best = std::max_element(marks.begin(), marks.end());   // iterator
    std::cout << "Highest: " << *best << "\n";

    int toppers = std::count(marks.begin(), marks.end(), 91);
    std::cout << "Students with 91: " << toppers << "\n";

    auto it = std::find(marks.begin(), marks.end(), 66);
    if (it != marks.end())
        std::cout << "66 found at position " << (it - marks.begin()) << "\n";
    return 0;
}
Sorted: 55 66 73 78 84 91 91 Highest: 91 Students with 91: 2 66 found at position 1

न manual memory management, न हाथ से लिखा sorting loop — और हर line बताती है कि वह क्या करती है।

Containers के प्रकार

Sequence containers — जिस क्रम में डालें उसी क्रम में

  • [vector](/tutorial/cpp/vector/?lang=hi) — dynamic array; index से तेज़ access, अंत में तेज़ जोड़ना/हटाना।
  • [deque](/tutorial/cpp/list-deque/?lang=hi) — double-ended queue; दोनों सिरों पर तेज़ जोड़ना/हटाना।
  • [list](/tutorial/cpp/list-deque/?lang=hi) — doubly linked list; iterator हो तो कहीं भी तेज़ insert/erase।
  • array — STL features वाला fixed-size array।
  • forward_list — singly linked list।

Associative containers — key के हिसाब से sorted

  • [set / multiset](/tutorial/cpp/set-map/?lang=hi) — sorted values (unique / duplicates allowed)।
  • [map / multimap](/tutorial/cpp/set-map/?lang=hi) — sorted key → value जोड़े।

Unordered containers — hash tables

  • unordered_set, unordered_map — औसतन O(1) lookup, कोई क्रम नहीं।

Container adapters — सीमित interface

  • [stack](/tutorial/cpp/stack-queue/?lang=hi) (LIFO), [queue](/tutorial/cpp/stack-queue/?lang=hi) (FIFO), [priority_queue](/tutorial/cpp/stack-queue/?lang=hi) (सबसे बड़ा पहले)।

कौन-सा container लें?

आपको चाहिए…लें
Items की सूची, ज़्यादातर अंत में जोड़ना, index से accessvector
आगे और पीछे तेज़ insert/removedeque
Unique items, sorted (जैसे roll numbers)set
Key से value खोजना, sorted key क्रम में (जैसे name → marks)map
Key से सबसे तेज़ lookup, क्रम ज़रूरी नहींunordered_map
Last-in-first-out (undo, bracket matching)stack
First-in-first-out (token queue, BFS)queue
हमेशा सबसे ऊँची/नीची priority अगलीpriority_queue

शक हो तो vector से शुरू करें।

दूसरा उदाहरण: map

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

int main() {
    std::map<std::string, int> marks;
    marks["Priya"] = 91;
    marks["Aman"]  = 78;
    marks["Ravi"]  = 66;

    for (const auto& [name, m] : marks)          // C++17 structured binding
        std::cout << name << " -> " << m << "\n";
    return 0;
}
Aman -> 78 Priya -> 91 Ravi -> 66

map नामों को अपने-आप alphabetical क्रम में रखता है।

STL क्यों इस्तेमाल करें?

  • सही: लाखों programmers इस्तेमाल करते हैं और compiler बनाने वाले इसे test करते हैं।
  • तेज़: algorithms की complexity तय है, जैसे sort O(n log n) है।
  • Generic: वही vector int, std::string या आपकी Student class के लिए चलता है।
  • कम code: कम loops और manual new/delete नहीं।
  • ज़रूरी ज्ञान: लगभग हर C++ interview, competitive programming और असली project में इस्तेमाल।

आम गलतियाँ

  • Header भूलना: <vector>, <map>, <algorithm> अलग-अलग include करने होते हैं।
  • end() को dereference करना — यह आख़िरी element के *बाद* की ओर इशारा करता है, उस पर नहीं।
  • map पर map.find(key) की जगह उसकी range पर find चलाना (बहुत धीमा)।
  • Iterators पकड़े रहते हुए vector बदलना (जैसे push_back) — वे invalid हो सकते हैं।
  • Key है या नहीं, यह check करने के लिए map पर operator[] लगाना; यह key insert कर देता है। find या count लें।
🏋️ अभ्यास

8 marks vector<int> में पढ़ें। उन्हें घटते क्रम में print करें (संकेत: std::greater<int>()), <numeric> के std::accumulate से average print करें, और lambda के साथ std::count_if से गिनें कि कितने 33 या उससे ज़्यादा हैं।

सारांश

  • STL तैयार containers, iterators, algorithms और function objects देती है।
  • Algorithms iterator ranges पर काम करते हैं, इसलिए container से स्वतंत्र हैं।
  • Sequence, associative, unordered और adapter containers अलग-अलग कामों के लिए हैं।
  • vector से शुरू करें; key से lookup के लिए map/unordered_map।
  • सही header include करें और end() को कभी dereference न करें।

अगला: vector → list और deque → stack और queue → set और map → iterators → algorithms।

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

C++ में STL क्या है?
STL (Standard Template Library) C++ standard library का वह हिस्सा है जो आम data structures (vector, list, map, set जैसे containers) और algorithms (sort, find, count जैसे) के लिए तैयार, tested templates देता है, जो iterators के ज़रिए साथ काम करते हैं।
STL के components कौन-से हैं?
मुख्य components हैं containers (data रखते हैं), iterators (data में चलते हैं), algorithms (data पर काम करते हैं) और function objects या lambdas (algorithms को customise करते हैं, जैसे sorting के लिए comparison)। stack और queue जैसे container adapters containers के ऊपर बने हैं।
STL में containers कितने प्रकार के होते हैं?
Sequence containers elements को क्रम में रखते हैं: vector, deque, list, forward_list, array। Associative containers key के हिसाब से sorted रखते हैं: set, multiset, map, multimap। Unordered containers hashing इस्तेमाल करते हैं: unordered_set, unordered_map। Container adapters: stack, queue, priority_queue।
Default रूप से कौन-सा STL container लें?
जब तक कोई ख़ास वजह न हो, std::vector लें। यह elements लगातार memory में रखता है, cache-friendly है, random access और अंत में insertion तेज़ है। Key–value lookup के लिए map/unordered_map, unique sorted values के लिए set, और दोनों सिरों पर तेज़ insertion के लिए deque चुनें।
← 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 दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicVector in C++ STL आपकी प्रगति: 53 / 81 (65%) · Lesson 54 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

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

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

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

▶ CodingEasily on YouTube

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

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

▶ Channel subscribe करें