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 हैं।
STL Architecture
| Part | काम | Examples |
|---|---|---|
| Container | Elements store/organize | vector, map |
| Iterator | Position/range | begin, end |
| Algorithm | Search/transform | sort, find_if |
| Callable | Behavior customize | lambda, comparator |
| Range | begin/end package | C++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 कैसे चुनें?
| Requirement | Choice | Property |
|---|---|---|
| General sequence | vector | Contiguous, O(1) index |
| दोनों ends | deque | Fast front/back |
| Fixed size | array | Compile-time size |
| Splice/stable positions | list | No random access |
| Sorted unique values | set | O(log n) |
| Sorted key-value | map | O(log n) |
| Hash lookup | unordered_map | Average 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;
}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());पहले total 404 और count 3 निकला। Sort duplicates adjacent करता है; unique logical end देता है और erase leftover tail हटाता है।
Mistakes और 100-Point Checklist
| Mistake | Correction |
|---|---|
| Habit से container | Operations/guarantees लिखें |
| end dereference | Excluded sentinel मानें |
| Stale iterator | Reacquire/check rule |
| Unsorted binary search | Ordering सुनिश्चित करें |
| unordered में order | Ordered container/sort |
| Header में using namespace | Qualified names |
- Data/ownership model
- Container guarantees
- Iterator requirement
- Algorithm precondition
- Invalidation audit
- Valid comparator
- Warnings enabled
- Edge-case tests
- Performance measurement
- Language standard recorded
Authoritative संदर्भ
Requirements और complexity current working draft से verified हैं। Beginner sequence के लिए STL introduction खोलें।
