C++ Standard Template Library (STL) Cheat Sheet

Below is a C++ Standard Template Library (STL) cheat sheet covering common containers, algorithms, and utilities:

Containers

Vector:

#include <vector>
std::vector<int> myVector = {1, 2, 3};

List:

#include <list>
std::list<int> myList = {4, 5, 6};

Deque:

#include <deque>
std::deque<int> myDeque = {7, 8, 9};

Queue:

#include <queue>
std::queue<int> myQueue;

Stack:

#include <stack>
std::stack<int> myStack;

Map:

#include <map>
std::map<std::string, int> myMap;

Set:

#include <set>
std::set<int> mySet;

Unordered Map:

#include <unordered_map>
std::unordered_map<std::string, int> myUnorderedMap;

Unordered Set:

#include <unordered_set>
std::unordered_set<int> myUnorderedSet;

Iterators

Begin and End:

auto it = myVector.begin();
auto endIt = myVector.end();

Advance Iterator:

std::advance(it, 2);

Algorithms

Sort:

#include <algorithm>
std::sort(myVector.begin(), myVector.end());

Reverse:

std::reverse(myList.begin(), myList.end());

Find:

auto result = std::find(mySet.begin(), mySet.end(), 42);

Count:

int count = std::count(myVector.begin(), myVector.end(), 3);

Max and Min:

int maxElement = *std::max_element(myVector.begin(), myVector.end());
int minElement = *std::min_element(myVector.begin(), myVector.end());

Transform:

std::transform(myVector.begin(), myVector.end(), myVector.begin(), [](int x) { return x * 2; });

Copy:

std::copy(myVector.begin(), myVector.end(), std::back_inserter(myDeque));

Utilities

Pair:

#include <utility>
std::pair<int, std::string> myPair = std::make_pair(42, "hello");

Tuple:

#include <tuple>
std::tuple<int, float, std::string> myTuple = std::make_tuple(42, 3.14f, "world");

Lambda Expressions:

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

Functional Objects (Functors):

struct MyFunctor {
    int operator()(int x) const { return x * 2; }
};

Smart Pointers:

#include <memory>
std::unique_ptr<int> uniquePtr = std::make_unique<int>(42);

String Conversion:

#include <sstream>
int myInt = std::stoi("42");

This cheat sheet provides a quick reference for common C++ STL containers, algorithms, and utilities. The STL is a powerful and extensive library, and this cheat sheet covers only the basics. Refer to the C++ reference documentation for more detailed information and advanced features.