Standard Template Library (STL) in C++

The Standard Template Library (STL) is a powerful collection of ready-to-use, generic classes and functions built into C++. It provides efficient implementations of common data structures (like arrays, lists, and maps) and algorithms (like sorting and searching), all built using templates so they work with any data type.

Why Use STL?

  • Ready-made: No need to write data structures from scratch.
  • Efficient: STL containers and algorithms are optimized and tested.
  • Generic: Works with any data type thanks to templates.
  • Standardized: Available on every C++ compiler without external libraries.

Main Components of STL

ComponentDescriptionExamples
ContainersData structures that store collections of objectsvector, list, map, set, queue, stack
IteratorsObjects to traverse containersbegin(), end(), iterator
AlgorithmsFunctions to operate on containerssort(), find(), count(), reverse()
Function objectsObjects that act like functions (functors)greater<T>, less<T>

STL Container Categories

Sequence Containers — Elements in linear order:

  • vector — Dynamic resizable array
  • list — Doubly linked list
  • deque — Double-ended queue
  • array — Fixed-size array (C++11)

Associative Containers — Elements sorted by key:

  • map — Key-value pairs, sorted by key
  • set — Unique values, sorted automatically
  • multimap — Like map, but allows duplicate keys
  • multiset — Like set, but allows duplicates

Unordered Containers — Hash-based, faster lookup (C++11):

  • unordered_map
  • unordered_set

Container Adaptors — Built on top of other containers:

  • stack — LIFO structure
  • queue — FIFO structure
  • priority_queue — Sorted access

Quick Overview — Using STL Containers

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

int main() {
    vector<int> nums = {5, 2, 8, 1, 9, 3};

    // Sort using STL algorithm
    sort(nums.begin(), nums.end());

    // Print using iterator
    for (int n : nums) {
        cout << n << " ";
    }
    cout << endl;

    // Find element
    auto it = find(nums.begin(), nums.end(), 8);
    if (it != nums.end())
        cout << "Found: " << *it << endl;

    return 0;
}

Output:

1 2 3 5 8 9
Found: 8

Common STL Algorithms

AlgorithmHeaderDescription
sort()<algorithm>Sort elements
find()<algorithm>Find first matching element
count()<algorithm>Count occurrences
reverse()<algorithm>Reverse elements
min_element()<algorithm>Find minimum element
max_element()<algorithm>Find maximum element
accumulate()<numeric>Sum of elements
fill()<algorithm>Fill with a value
binary_search()<algorithm>Check if value exists (sorted)

Iterators

Iterators are like smart pointers that allow traversal over containers regardless of their internal structure:

vector<int> v = {10, 20, 30};

// Using iterator explicitly
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++) {
    cout << *it << " ";
}

Output:

10 20 30

Key Takeaways

  • STL provides ready-made, efficient, generic data structures and algorithms.
  • STL has three main parts: containers, iterators, and algorithms.
  • Containers store data; iterators traverse it; algorithms operate on it.
  • STL greatly reduces the amount of code needed for common programming tasks.
  • Always include the appropriate header for each STL component.

Leave a Comment

Your email address will not be published. Required fields are marked *