STL Overview

Introduction to containers, iterators, algorithms

Interview Relevant: Foundation for STL

STL Overview

Standard Template Library provides containers, iterators, and algorithms.

Code Examples

Core components of the STL.

cpp
1// Containers - store data
2vector<int> v;        // Dynamic array
3list<int> l;          // Doubly linked list
4map<string, int> m;   // Key-value pairs
5set<int> s;           // Unique sorted elements
6
7// Iterators - traverse containers
8for (auto it = v.begin(); it != v.end(); ++it) {
9    cout << *it << " ";
10}
11
12// Algorithms - operate on ranges
13sort(v.begin(), v.end());
14auto it = find(v.begin(), v.end(), 42);
15int count = count_if(v.begin(), v.end(), [](int x) { return x > 0; });
16
17// Functors - function objects
18struct Compare {
19    bool operator()(int a, int b) { return a > b; }
20};
21sort(v.begin(), v.end(), Compare());

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In