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
- 9, newest first
- Written for
- C++17, C++20, C++23
- 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 9, newest first
C++ int to string:
to_string, format orto_chars, not itoaConvert an int to a string in C++ with
std::to_string,std::formatorstd::to_chars: the exact output of each, what allocates, and why itoa is not standard.written for C++17
C++ string to int:
std::stoiorstd::from_chars, not atoiConvert a string to int in C++ with
std::stoiorstd::from_chars, reject trailing text, and see what each call, atoi included, does with bad input.written for C++17
How to write comparators for
std::sortandstd::mapstd::sort,std::setandstd::mapneed a strict weak ordering: the three rules, what breaks when one fails, and the comparison C++20’s<=>writes for you.written for C++20
std::expectedin C++23: return a value or an errorReturn a value or an error with
std::expected(C++23):std::unexpected, what each accessor does in the wrong state, chaining, choosing E, and when to use it.written for C++23
C++ map:
std::mapfind, insert, erase and iterateUse a C++ map: look a key up with find, contains or at, add one with
try_emplace, erase in a loop, and see whatstd::mapdoes when a key is missing.written for C++17
std::optionalin C++: how to use it and when not toCreate, test, read, empty and refill a
std::optional, what each accessor does when it is empty, what it costs, and whenstd::expectedfits better.written for C++17
std::printlnin C++23: print withoutstd::coutor printfstd::printlnin C++23: the syntax, eachstd::coutand printf line translated, what a bad format string does, whether it flushes, andstd::endlvs newline.written for C++23
std::spanin C++20: pass a view, not a pointer and a sizeTake
std::spanby value: what converts to it, slicing with subspan, what s[i] out of range does on each library, and how a span dangles.written for C++20
std::variantin C++:std::visit,std::getand the trapsCreate a
std::variant, read it withstd::get,get_iforstd::visit, catch the missing case a conversion hides, and see what it costs and when to skip it.written for C++17