C++ STL Cheat Sheet

The Standard Template Library (STL) is a powerful set of C++ template classes that provide general-purpose classes and functions with pre-implemented algorithms and data structures. Mastering STL containers, iterators, and algorithmic complexity is essential for competitive programming, systems architecture, and technical interviews.

This comprehensive, developer-focused C++ STL Cheat Sheet covers sequence containers, associative and unordered containers, iterators, and high-performance algorithms.

1. Sequence Containers (vector, list, deque)

Sequence containers store objects in a strictly linear arrangement.

C
#include <iostream>
#include <vector>
#include <deque>

int main() {
    // std::vector: Dynamic contiguous array (O(1) random access, O(N) insertion in middle)
    std::vector<int> vec = {10, 20, 30};
    vec.push_back(40);
    vec.pop_back();

    // std::deque: Double-ended queue (O(1) insertion/deletion at both ends)
    std::deque<int> dq = {1, 2, 3};
    dq.push_front(0);
    dq.push_back(4);
    
    std::cout << "Vector size: " << vec.size() << " | Deque front: " << dq.front() << "\n";
    return 0;
}

2. Associative Containers (map, set, multimap, multiset)

Associative containers implement sorted data structures that can be quickly searched (O(log N) complexity via Red-Black Trees).

C
#include <iostream>
#include <map>
#include <set>

int main() {
    // std::map: Key-Value pairs sorted by unique keys (O(log N))
    std::map<std::string, int> scores;
    scores["Alice"] = 95;
    scores["Bob"] = 88;

    // std::set: Sorted collection of unique elements
    std::set<int> uniqueIds = {5, 1, 3, 3, 2};
    // Elements stored in sorted order: 1, 2, 3, 5

    for (int id : uniqueIds) {
        std::cout << id << " ";
    }
    return 0;
}

3. Unordered Containers (unordered_map, unordered_set)

Unordered containers use hash tables to provide average O(1) time complexity for search, insertion, and deletion.

C
#include <iostream>
#include <unordered_map>
#include <unordered_set>

int main() {
    // std::unordered_map: Hash map (O(1) average lookup)
    std::unordered_map<int, std::string> cache;
    cache[101] = "SessionActive";
    cache[102] = "SessionExpired";

    if (cache.find(101) != cache.end()) {
        std::cout << "Found key 101: " << cache[101] << "\n";
    }
    return 0;
}

4. Essential STL Algorithms (sort, find, binary_search)

The <algorithm> header provides efficient, standardized operations on container iterators.

C
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> nums = {45, 12, 89, 23, 56};

    // Sorting ascending
    std::sort(nums.begin(), nums.end());

    // Binary search (Requires sorted range)
    bool found = std::binary_search(nums.begin(), nums.end(), 23);
    std::cout << "Element 23 found: " << (found ? "Yes" : "No") << "\n";

    // Finding maximum element
    auto max_it = std::max_element(nums.begin(), nums.end());
    std::cout << "Max element: " << *max_it << "\n";
    return 0;
}

Conclusion

Keep this C++ STL reference cheat sheet handy to quickly choose the optimal container (vector, map, unordered_map) and algorithmic functions for high-performance software engineering.