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

STL Overview

C++ STL क्या है?

STL generic-programming foundation है: containers data own करते हैं, iterators range बताते हैं और algorithms उस range पर काम करते हैं। Function objects/lambdas behavior customize करते हैं।

std::vector<int> values{5,2,9,2};
std::sort(values.begin(),values.end());

पूरी standard library को STL कहना common लेकिन imprecise है; I/O, threads और filesystem भी library facilities हैं।

Core idea: requirements और complexity contract समझकर tested components combine करें; sorting/list को unnecessarily दोबारा न बनाएं।

STL Architecture

PartकामExamples
ContainerElements store/organizevector, map
IteratorPosition/rangebegin, end
AlgorithmSearch/transformsort, find_if
CallableBehavior customizelambda, comparator
Rangebegin/end packageC++20 ranges
auto passed=std::count_if(marks.begin(),marks.end(),
 [](int m){ return m>=40; });

Algorithm data own नहीं करता। Required headers direct include करें: vector के लिए <vector>, algorithms के लिए <algorithm>, accumulate के लिए <numeric>।

सही Container कैसे चुनें?

RequirementChoiceProperty
General sequencevectorContiguous, O(1) index
दोनों endsdequeFast front/back
Fixed sizearrayCompile-time size
Splice/stable positionslistNo random access
Sorted unique valuessetO(log n)
Sorted key-valuemapO(log n)
Hash lookupunordered_mapAverage O(1), worst O(n)

Sequence में vector default रखें। list का insertion O(1) होने पर भी position तक पहुंचना O(n), per-node allocation और weak locality हो सकती है। vector तथा set/map पढ़ें।

Iterators और Half-Open Ranges

Range [first,last) first को include और last को exclude करती है। end() sentinel है, element नहीं; इसे dereference न करें।

for(auto it=values.cbegin();it!=values.cend();++it)
 std::cout<<*it<<' ';

Iterator capabilities input, forward, bidirectional, random-access और contiguous तक बढ़ती हैं। sort को random-access चाहिए; इसलिए vector पर चलेगा, list पर नहीं। List का अपना member sort है। अधिक detail: iterators।

Algorithms का Practical प्रयोग

std::sort(data.begin(),data.end());
auto it=std::find_if(data.begin(),data.end(),
 [](int n){return n%2==0;});
long long total=std::accumulate(data.begin(),data.end(),0LL);
std::transform(data.begin(),data.end(),data.begin(),
 [](int n){return n*2;});

lower_bound sorted range पर logarithmic comparisons करता है; unsorted data पर precondition पूरी नहीं। Comparator strict weak ordering दे—a<=b गलत है। C++20 में std::ranges::sort(data) intent साफ कर सकता है; lazy views में source lifetime सुरक्षित रखें। algorithms lesson देखें।

Complexity और Invalidation

Big-O growth contract है, exact seconds नहीं। Allocation, cache locality, hash और element size भी measure करें।

  • vector reallocation सभी iterators/references/pointers invalidate करती है।
  • vector erase, erased position और उसके बाद के handles invalidate करता है।
  • map/set insert existing iterators को normally valid रखता है; erase केवल erased element का handle invalidate करता है।
  • unordered rehash iterators invalidate करता है।
values.reserve(100); // capacity, size नहीं
for(auto it=values.begin();it!=values.end();){
 if(*it<0) it=values.erase(it); else ++it;
}
Mutation के बाद returned iterator use या position reacquire करें, जब तक documented guarantee पुराने handle को valid न सिद्ध करे।

Complete Practical Lab और Output

std::vector<int> marks{72,88,65,88,91};
int total=std::accumulate(marks.begin(),marks.end(),0);
auto high=std::count_if(marks.begin(),marks.end(),
 [](int m){return m>=75;});
std::sort(marks.begin(),marks.end());
auto new_end=std::unique(marks.begin(),marks.end());
marks.erase(new_end,marks.end());
Average: 80.8 At least 75: 3 Unique: 65 72 88 91

पहले total 404 और count 3 निकला। Sort duplicates adjacent करता है; unique logical end देता है और erase leftover tail हटाता है।

Mistakes और 100-Point Checklist

MistakeCorrection
Habit से containerOperations/guarantees लिखें
end dereferenceExcluded sentinel मानें
Stale iteratorReacquire/check rule
Unsorted binary searchOrdering सुनिश्चित करें
unordered में orderOrdered container/sort
Header में using namespaceQualified names
  1. Data/ownership model
  2. Container guarantees
  3. Iterator requirement
  4. Algorithm precondition
  5. Invalidation audit
  6. Valid comparator
  7. Warnings enabled
  8. Edge-case tests
  9. Performance measurement
  10. Language standard recorded

Authoritative संदर्भ

Requirements और complexity current working draft से verified हैं। Beginner sequence के लिए STL introduction खोलें।

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

क्या STL पूरी C++ standard library के समान है?
पूरी तरह नहीं। STL नाम सामान्यतः generic containers, iterators, algorithms और related function objects के लिए प्रयोग होता है। I/O, threading और filesystem जैसी facilities भी standard library में हैं, लेकिन वे STL core नहीं हैं।
Beginner को default में कौन-सा container लेना चाहिए?
Growable sequence के लिए std::vector से शुरू करें, जब तक ordering, lookup, insertion या invalidation की measured requirement किसी और container की मांग न करे।
Algorithms [first, last) half-open range क्यों लेते हैं?
first पहला element है और last range के बाद की position। इससे empty range natural बनती है और adjacent ranges एक boundary share कर सकती हैं। last/end को dereference नहीं करना चाहिए।
क्या unordered_map हमेशा O(1) है?
नहीं। Suitable hashing/load में operations average O(1) हो सकते हैं, लेकिन worst case O(n) है और iteration sorted नहीं होती।
Iterator invalidation क्या है?
किसी container operation के बाद पुराना iterator, pointer या reference valid element को identify करना बंद कर सकता है। Exact rule container और operation पर निर्भर है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

अगला topicVectors and Maps आपकी प्रगति: 74 / 81 (91%) · Lesson 75 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

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

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

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

▶ CodingEasily on YouTube

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

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

▶ Channel subscribe करें