STL Overview
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What Is the C++ STL?
The Standard Template Library (STL) is the generic-programming foundation commonly used to describe the standard library's containers, iterators, algorithms and function objects. These components are designed to cooperate: a container stores values, iterators describe a range, and an algorithm operates on that range without needing to know the container's concrete type.
#include <algorithm>
#include <vector>
std::vector<int> values{5, 2, 9, 2};
std::sort(values.begin(), values.end());STL is often used informally for the whole C++ standard library, but that is imprecise. Streams, strings, regular expressions, threads and the filesystem are standard-library facilities; the classic STL model is the generic container–iterator–algorithm system.
How the STL Architecture Fits Together
| Component | Responsibility | Examples |
|---|---|---|
| Container | Owns and organizes elements | vector, map, set |
| Iterator | Identifies a position/range | begin(), end() |
| Algorithm | Transforms, searches or measures a range | sort, find_if |
| Callable | Customizes an algorithm | lambda, comparator, predicate |
| Range (C++20) | Packages a begin/end range | std::ranges::sort |
const auto passed = std::count_if(
marks.begin(), marks.end(),
[](int mark) { return mark >= 40; }
);The algorithm accepts an iterator range and a predicate. It does not own the data. This separation makes the same algorithm reusable with many containers whose iterators satisfy its requirements.
Headers matter: <vector> declares vector, <algorithm> declares sorting/search algorithms, and <numeric> declares accumulate. Include what the source directly uses.
Choosing the Right Container
| Need | Usually choose | Important property |
|---|---|---|
| General growable sequence | vector | Contiguous; O(1) indexing; amortized O(1) push-back |
| Fast insertion at both ends | deque | O(1) front/back operations; not one contiguous block |
| Fixed compile-time size | array | Contiguous; size is part of its type |
| Frequent splice with stable positions | list | No random access; extra nodes and poor locality |
| Sorted unique values | set | Ordered; logarithmic search/insert/erase |
| Sorted key-value records | map | Unique ordered keys; logarithmic operations |
| Hash lookup, order unneeded | unordered_map | Average O(1), worst O(n); rehash considerations |
| LIFO/FIFO restricted interface | stack/queue | Container adapters, not general traversal containers |
vector is the default sequence because compact contiguous elements exploit caches well. Do not select list merely because insertion is O(1): reaching the position is O(n), allocation is per node, and real workloads often favor vector.
See the focused lessons on vector and set and map.
Iterators, Categories and Half-Open Ranges
An iterator behaves like a generalized position. A range is normally [first, last): it includes first and excludes last. For a complete container, use begin() and end(); end() is a sentinel position and must not be dereferenced.
for (auto it = values.cbegin(); it != values.cend(); ++it) {
std::cout << *it << ' ';
}| Capability | Typical source | Permits |
|---|---|---|
| Input | readable stream/range | single-pass reading |
| Forward | forward_list | multi-pass forward movement |
| Bidirectional | list, map | ++ and -- |
| Random access | deque | jumps, differences, ordering |
| Contiguous | vector, array | adjacent storage plus random access |
std::sort requires random-access iterators, so it works with vector but not list. A list supplies its own list::sort. Study C++ iterators before keeping them across mutations.
Algorithms in Practice
#include <algorithm>
#include <numeric>
#include <vector>
std::vector<int> data{7, 2, 9, 4};
std::sort(data.begin(), data.end());
auto first_even = std::find_if(data.begin(), data.end(),
[](int n) { return n % 2 == 0; });
int total = std::accumulate(data.begin(), data.end(), 0);
std::transform(data.begin(), data.end(), data.begin(),
[](int n) { return n * 2; });The initial value in accumulate also helps determine the result type. Use 0LL when summing many integers into long long. A comparator must define a valid strict weak ordering; writing a <= b for sort is wrong.
If a vector is already sorted, lower_bound performs logarithmic comparisons. Calling it on an unsorted range produces no useful search guarantee. C++20 ranges can make intent clearer:
#include <ranges>
std::ranges::sort(data);
auto odds = data | std::views::filter([](int n) { return n % 2; });Views are often lazy and may refer to another range; manage source lifetimes carefully. Continue with STL algorithms.
Complexity, Allocation and Invalidation
Big-O is a growth contract, not a stopwatch result. O(1) can have a large constant; O(n) over contiguous memory may beat pointer-heavy traversal for practical sizes. Evaluate element size, allocation, hashing, cache locality and the actual input distribution.
- vector growth: when capacity is exhausted, reallocation moves/copies elements and invalidates all iterators, pointers and references.
- vector erase: handles at or after the erased position are invalidated.
- map/set insert: existing iterators and references normally remain valid; erasing an element invalidates handles to that element.
- unordered rehash: iterators are invalidated by rehash; references/pointers to elements remain valid under the standard rules unless the element is erased.
values.reserve(100); // capacity changes, size does not
values.push_back(42); // do not retain stale iterators blindly
for (auto it = values.begin(); it != values.end(); ) {
if (*it < 0) it = values.erase(it);
else ++it;
}Complete STL Lab with Verified Output
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> marks{72, 88, 65, 88, 91};
const int total = std::accumulate(marks.begin(), marks.end(), 0);
const auto high = std::count_if(marks.begin(), marks.end(),
[](int m) { return m >= 75; });
std::sort(marks.begin(), marks.end());
const auto new_end = std::unique(marks.begin(), marks.end());
marks.erase(new_end, marks.end());
std::cout << std::fixed << std::setprecision(1)
<< "Average: " << total / 5.0 << '\n'
<< "At least 75: " << high << "\nUnique: ";
for (int mark : marks) std::cout << mark << ' ';
}The erase–unique idiom has two stages: unique compacts adjacent duplicates and returns the new logical end; erase removes the leftover tail. Sorting first makes equal values adjacent.
Common Mistakes and 100-Point Checklist
| Mistake | Faculty correction |
|---|---|
| Choosing a container by habit | Write access, order and invalidation needs first |
Dereferencing end() | Treat it as an excluded sentinel |
| Keeping an iterator after reallocation | Reacquire it or prove validity |
| Running binary search on unsorted input | Maintain/verify the required ordering |
| Expecting unordered iteration order | Use ordered container or sort an extracted view |
using namespace std; in a header | Use qualified names; avoid namespace pollution |
| Missing direct headers | Include every facility used |
| Unsafe index access | Validate index or use at() where appropriate |
- State the data and ownership model.
- Select a container from operations and guarantees.
- Check algorithm iterator requirements.
- Check preconditions such as sorting.
- Account for invalidation after mutation.
- Use a valid comparator/predicate.
- Compile with
-Wall -Wextra -Wpedantic. - Test empty, one-element, duplicate and large cases.
- Measure before performance claims.
- Record the required language standard.
Authoritative References
- C++ Working Draft: Containers Library
- C++ Working Draft: Iterators Library
- C++ Working Draft: Algorithms Library
The requirements, complexity terminology and invalidation guidance on this page were checked against the current C++ working draft. For a shorter entry point, open STL introduction.
Frequently Asked Questions
Is the STL the same as the complete C++ standard library?
Which STL container should a beginner choose by default?
Why do algorithms use a half-open range [first, last)?
Are unordered_map operations always O(1)?
What is iterator invalidation?
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 खोलें।