C++ STL Cheat Sheet

In C++

C++ STL (Standard Template Library) is a powerful library that provides a set of generic algorithms, containers, and iterators for C++ programming language. It was introduced in the C++98 standard and has been an integral part of the language ever since.

The STL is designed to provide a high level of abstraction, allowing developers to write efficient and reusable code. It includes a wide range of containers such as vectors, lists, maps, sets, and more. These containers are designed to store and manipulate data efficiently, and they provide a range of operations such as insertion, deletion, and searching.

The algorithms provided by the STL are also highly efficient and can be used with any container. They include sorting, searching, and manipulating algorithms, among others. These algorithms are designed to work with iterators, which are used to traverse the elements of a container.

One of the key benefits of using the STL is that it provides a consistent and standardized interface for working with containers and algorithms. This makes it easier for developers to write code that is portable and can be used across different platforms and compilers.

Overall, the C++ STL is a powerful library that provides a range of containers, algorithms, and iterators for C++ programming. It is widely used in industry and academia and is an essential tool for any C++ developer.

Containers

NameDescription
std::vectorA dynamic array that can be resized at runtime.
std::listA doubly linked list.
std::dequeA double-ended queue.
std::setA sorted set of unique elements.
std::mapA sorted map of key-value pairs.
std::unordered_setAn unordered set of unique elements.
std::unordered_mapAn unordered map of key-value pairs.

Iterators

NameDescription
std::beginReturns an iterator to the beginning of a container.
std::endReturns an iterator to the end of a container.
std::advanceAdvances an iterator by a specified number of positions.
std::distanceReturns the distance between two iterators.

Algorithms

NameDescription
std::sortSorts a range of elements.
std::findFinds the first occurrence of a value in a range.
std::countCounts the number of occurrences of a value in a range.
std::transformApplies a function to a range of elements and stores the result in another range.
std::accumulateComputes the sum of a range of elements.

Strings

NameDescription
std::stringA string class that can be resized at runtime.
std::getlineReads a line of input from a stream.
std::stoiConverts a string to an integer.
std::stodConverts a string to a double.
std::to_stringConverts a value to a string.

Memory Management

NameDescription
std::unique_ptrA smart pointer that owns and manages a dynamically allocated object.
std::shared_ptrA smart pointer that allows multiple pointers to share ownership of an object.
std::make_uniqueCreates a new std::unique_ptr with a dynamically allocated object.
std::make_sharedCreates a new std::shared_ptr with a dynamically allocated object.

Miscellaneous

NameDescription
std::pairA class that holds two values of different types.
std::tupleA class that holds multiple values of different types.
std::chronoA library for working with time and dates.
std::random_deviceA class for generating random numbers.
std::threadA class for creating and managing threads.

Conclusion

This cheat sheet provides a quick reference for the most commonly used C++ STL functions and classes. However, it is not an exhaustive list, and there are many other functions and classes available in the STL. For more information, consult the C++ documentation or a comprehensive C++ reference book.

Reference:

https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-reference?view=msvc-170

#