C++ Standard Template Library (STL) | Containers, Iterators, Algorithms, Functors, Lambda Expressions


This complete tutorial on C++ Standard Template Library (STL) explains containers, iterators, algorithms, and function objects. It covers vectors, lists, maps, sets, stacks, queues, iterators, STL algorithms, and functors, lambda expressions, and custom comparators. Following best practices, it helps learners write efficient and reusable C++ code using STL.

Standard Template Library (STL) – Complete Tutorial

1. What is STL?

STL is a powerful library in C++ that provides generic classes and functions for data structures and algorithms.

  1. Consists of Containers, Iterators, Algorithms, and Function Objects
  2. Provides type-safe, reusable, and efficient code

2. Containers

Containers store collections of data. They are classified as:

a) Sequence Containers

  1. Store elements in linear order
  2. Examples: vector, list, deque, stack, queue

Vector


#include <vector>
#include <iostream>
using namespace std;

int main() {
vector<int> v = {1, 2, 3};
v.push_back(4);
for(int i : v) cout << i << " ";
}

List


#include <list>
#include <iostream>
using namespace std;

int main() {
list<int> l = {1, 2, 3};
l.push_back(4);
for(int i : l) cout << i << " ";
}

Deque

  1. Double-ended queue
  2. Allows insertion/removal at both ends

#include <deque>

Stack

  1. LIFO (Last In First Out)

#include <stack>

Queue

  1. FIFO (First In First Out)

#include <queue>

Priority Queue

  1. Max-heap or Min-heap

#include <queue>
priority_queue<int> pq;

b) Associative Containers

  1. Store elements as key-value pairs or sorted sets
  2. Examples: set, multiset, map, multimap

Set


#include <set>
set<int> s = {3,1,2}; // automatically sorted

Map


#include <map>
map<string,int> m;
m["apple"] = 10;

Multiset and Multimap

  1. Allow duplicate elements

Unordered Set and Map

  1. Use hash table, unsorted, faster lookup

3. Iterators

Iterators are objects that point to elements in containers.

Types of Iterators

  1. Input Iterator – read-only
  2. Output Iterator – write-only
  3. Forward Iterator – read/write, forward only
  4. Bidirectional Iterator – forward & backward
  5. Random Access Iterator – jump anywhere, e.g., vector

Iterator Operations

  1. begin(), end(), rbegin(), rend()
  2. ++it, --it, *it for traversal

4. Algorithms

STL provides predefined algorithms:

Sorting


#include <algorithm>
vector<int> v = {3,1,2};
sort(v.begin(), v.end());

Searching

  1. find(), binary_search(), count()

Numeric Algorithms

  1. accumulate(), partial_sum(), iota()

Other Algorithms

  1. reverse(), rotate(), max_element(), min_element()

5. Function Objects

Function objects (functors) allow objects to behave like functions.

Functors


struct Multiply {
int operator()(int a, int b) { return a*b; }
};
Multiply mul;
cout << mul(2,3);

Lambda Expressions

  1. Introduced in C++11 for inline anonymous functions

auto add = [](int a,int b){ return a+b; };
cout << add(5,3);

Custom Comparators

  1. Used in sort, priority_queue, set/map

struct Compare {
bool operator()(int a, int b) { return a > b; }
};
priority_queue<int, vector<int>, Compare> pq;

Best Practices

  1. Use vectors for dynamic arrays
  2. Prefer iterators over indices for generic code
  3. Use associative containers for fast lookups
  4. Use lambda expressions for simple custom operations
  5. Avoid raw pointers; use STL containers for memory safety

Common Mistakes

  1. Using erase() incorrectly with iterators
  2. Modifying container while iterating (except carefully)
  3. Using wrong iterator type for algorithm
  4. Ignoring performance differences between containers

Summary

In this chapter, you learned about C++ STL, including:

  1. Sequence and associative containers
  2. Iterators and iterator operations
  3. Sorting, searching, and numeric algorithms
  4. Functors, lambda expressions, and custom comparators

STL allows writing efficient, reusable, and readable C++ code and is essential for competitive programming and real-world applications.