100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

C++ STL Cheat Sheet

C++ STL Cheat Sheet

References the C++ Standard Template Library covering vectors, maps, sets, iterators, algorithms, and container time complexity trade-offs.

3 PagesIntermediateMar 28, 2026

Common Containers

Frequently used STL container declarations and operations.

cpp
#include <vector>#include <map>#include <unordered_map>#include <set>std::vector<int> v = {1, 2, 3};v.push_back(4);                    // O(1) amortizedv.pop_back();                      // O(1)std::map<std::string, int> m;      // sorted, red-black tree, O(log n) opsm["apple"] = 3;m.insert({"banana", 5});std::unordered_map<std::string, int> um;  // hash table, O(1) avg opsum["cherry"] = 7;std::set<int> s = {3, 1, 2};       // sorted, unique elementss.insert(4);

Iterators & <algorithm>

Iterate and transform containers with the STL algorithm library.

cpp
#include <algorithm>#include <numeric>std::vector<int> v = {5, 3, 1, 4, 2};std::sort(v.begin(), v.end());                       // ascending sortstd::sort(v.begin(), v.end(), std::greater<int>());  // descending sortauto it = std::find(v.begin(), v.end(), 3);            // linear searchbool has4 = std::binary_search(v.begin(), v.end(), 4);  // requires sorted rangeint sum = std::accumulate(v.begin(), v.end(), 0);auto max_it = std::max_element(v.begin(), v.end());v.erase(std::remove(v.begin(), v.end(), 3), v.end()); // erase-remove idiom

Lambdas with Algorithms

Pass inline function objects into STL algorithms.

cpp
std::vector<int> v = {1, 2, 3, 4, 5};std::for_each(v.begin(), v.end(), [](int n) { std::cout << n << " "; });int threshold = 3;auto count = std::count_if(v.begin(), v.end(),        [threshold](int n) { return n > threshold; });   // capture by valuestd::transform(v.begin(), v.end(), v.begin(),        [](int n) { return n * n; });                    // square each elementstd::sort(v.begin(), v.end(),        [](int a, int b) { return a > b; });              // custom comparator

Choosing a Container

Time complexity guide for common operations.

  • vector- Contiguous array; O(1) random access, O(1) amortized push_back, O(n) insert/erase in the middle.
  • deque- Double-ended queue; O(1) push/pop at both ends, O(1) random access.
  • list- Doubly linked list; O(1) insert/erase anywhere with an iterator, no random access.
  • map / set- Balanced tree, O(log n) insert/find/erase, keeps keys sorted.
  • unordered_map / unordered_set- Hash table, average O(1) insert/find/erase, no ordering guarantee.
  • priority_queue- Binary heap adaptor; O(log n) push/pop, O(1) access to the max element.
Pro Tip

Reserve capacity with vector::reserve(n) when the final size is known up front - it avoids repeated reallocation and element copying/moving as the vector grows.

Was this cheat sheet helpful?

Explore Topics

#CSTL#CSTLCheatSheet#Programming#Intermediate#CommonContainers#IteratorsAlgorithm#LambdasWithAlgorithms#ChoosingAContainer#Algorithms#Docker#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet