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

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_view and formatting
  • Smart pointers and ownership
  • optional, variant, expected and any
  • Comparators, ordering and sorting
  • chrono, filesystem and random
  • 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

  1. C++ int to string: to_string, format or to_chars, not itoa

    Convert an int to a string in C++ with std::to_string, std::format or std::to_chars: the exact output of each, what allocates, and why itoa is not standard.

    §2.16 min readC++17

    written for C++17

  2. C++ string to int: std::stoi or std::from_chars, not atoi

    Convert a string to int in C++ with std::stoi or std::from_chars, reject trailing text, and see what each call, atoi included, does with bad input.

    §2.211 min readC++17

    written for C++17

  3. How to write comparators for std::sort and std::map

    std::sort, std::set and std::map need a strict weak ordering: the three rules, what breaks when one fails, and the comparison C++20’s <=> writes for you.

    §2.36 min readC++20

    written for C++20

  4. std::expected in C++23: return a value or an error

    Return 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.

    §2.411 min readC++23

    written for C++23

  5. C++ map: std::map find, insert, erase and iterate

    Use a C++ map: look a key up with find, contains or at, add one with try_emplace, erase in a loop, and see what std::map does when a key is missing.

    §2.511 min readC++17

    written for C++17

  6. std::optional in C++: how to use it and when not to

    Create, test, read, empty and refill a std::optional, what each accessor does when it is empty, what it costs, and when std::expected fits better.

    §2.611 min readC++17

    written for C++17

  7. std::println in C++23: print without std::cout or printf

    std::println in C++23: the syntax, each std::cout and printf line translated, what a bad format string does, whether it flushes, and std::endl vs newline.

    §2.77 min readC++23

    written for C++23

  8. std::span in C++20: pass a view, not a pointer and a size

    Take std::span by value: what converts to it, slicing with subspan, what s[i] out of range does on each library, and how a span dangles.

    §2.810 min readC++20

    written for C++20

  9. std::variant in C++: std::visit, std::get and the traps

    Create a std::variant, read it with std::get, get_if or std::visit, catch the missing case a conversion hides, and see what it costs and when to skip it.

    §2.99 min readC++17

    written for C++17