Sections[lib]
Standard Library
The library that ships with every compiler, from containers and algorithms to ranges and smart pointers, for developers choosing which type to reach for first.
- Clause
- 2 of 8
- Answers
- 42, newest first
- Page
- 1 of 5
- Written for
- C++11, C++17
- Written by
- Miriam Castellanos
The standard library is one specification with three main implementations (libstdc++, libc++ and Microsoft’s STL), and each adds a standard’s types at its own pace. This section covers the containers, algorithms and vocabulary types, which one fits a job, and when Boost or another library does better. Start with how to write comparators for std::sort and std::map: break the strict weak ordering they need and std::sort has undefined behavior, while std::set and std::map quietly merge keys you meant to keep apart.
In this section
- Containers and choosing between them
- Algorithms and iterators
- Ranges and views
- Strings,
string_viewand formatting - Smart pointers and ownership
optional,variant,expectedandany- Comparators, ordering and sorting
chrono,filesystemandrandom- Input, output and
print - Boost, and when the standard library is not enough
- Third-party libraries worth knowing
Answers in Standard Library 42, newest first
std::listExplained: How Doubly Linked Lists Workstd::listlinks elements in both directions, so you can insert without shifting neighbors. See its core operations, runnable example, and when vector is faster.Binary search in C++: code and
std::binary_searchBinary search in C++: a loop that returns the index, the recursive version,
std::binary_searchandlower_bound, and why the range must be sorted.written for C++17
cin in C++: read numbers, words and lines
cin in C++ reads what the user types: one value or several with >>, a whole line with getline, and how to fix cin after a letter breaks the read.
written for C++17
C++ cout: how to print with
std::coutC++ cout prints text, numbers and variables with
std::cout<<. End a line, fix the first errors, print two decimals with setprecision and print a bool.written for C++17
C++ data structures: which container to use
C++ data structures in one table: the standard library container for each, its header and its cost, how to choose one, and what the library leaves out.
written for C++17
C++ getline: how to read a whole line of input
C++ getline reads a whole line, spaces included, into a
std::string. Why it seems skipped after cin >>, both fixes, and how to read a file or split a line.written for C++11
C++ ifstream: how to open and read a file
C++ ifstream opens a file and reads it: a whole program that reads line by line, how to read words, numbers or all of it, and why your file does not open.
written for C++17
C++ iterator: how
begin()andend()workWhat a C++ iterator is and how to loop over a vector with one:
begin()andend(),std::nextandstd::distance, erasing in a loop, and when one goes stale.written for C++17
C++ linked list: write one, then use
std::listHow to write a C++ linked list that frees its nodes, insert and delete nodes, then use
std::listandstd::forward_list, and when a vector is the better pick.written for C++17
C++ pair: what
std::pairis and how to use itC++ pair explained: make a
std::pair, read first and second, compare two pairs, sort a vector of pairs and loop over the pairs in astd::map.written for C++17