Standard Template Library (STL)
STL क्या है?
मान लीजिए आपको कितने भी students के marks रखने हैं, उन्हें sort करना है और सबसे ज़्यादा marks खोजने हैं। C में आप अपना dynamic array, अपना sort और अपना search लिखेंगे — और तीनों को debug करेंगे। Standard Template Library (STL) यह सब तैयार, tested और तेज़ रूप में देती है:
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 | क्या करता है | उदाहरण |
|---|---|---|
| Containers | Objects का संग्रह रखते हैं | vector, list, deque, set, map, unordered_map |
| Iterators | Elements की ओर इशारा करते हैं और container में चलते हैं | begin(), end(), ++it, *it |
| Algorithms | Elements की range पर काम करते हैं | sort, find, count, reverse, max_element |
| Function objects / lambdas | Algorithms का व्यवहार बदलते हैं | std::greater<int>(), [](int a, int b){ return a > b; } |
मुख्य design विचार: algorithms containers के बारे में नहीं जानते। वे iterators की जोड़ी (range) पर काम करते हैं, इसलिए एक ही sort vector, deque या साधारण array पर चलता है।
पहला उदाहरण: रखना, sort करना, खोजना
#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;
}न 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 से access | vector |
| आगे और पीछे तेज़ insert/remove | deque |
| 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
#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;
}map नामों को अपने-आप alphabetical क्रम में रखता है।
STL क्यों इस्तेमाल करें?
- सही: लाखों programmers इस्तेमाल करते हैं और compiler बनाने वाले इसे test करते हैं।
- तेज़: algorithms की complexity तय है, जैसे
sortO(n log n) है। - Generic: वही
vectorint,std::stringया आपकीStudentclass के लिए चलता है। - कम 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 क्या है?
vector, list, map, set जैसे containers) और algorithms (sort, find, count जैसे) के लिए तैयार, tested templates देता है, जो iterators के ज़रिए साथ काम करते हैं।STL के components कौन-से हैं?
stack और queue जैसे container adapters containers के ऊपर बने हैं।STL में containers कितने प्रकार के होते हैं?
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 चुनें।