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

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 42, newest first

  1. std::list Explained: How Doubly Linked Lists Work

    std::list links elements in both directions, so you can insert without shifting neighbors. See its core operations, runnable example, and when vector is faster.

    §2.429 min read

  2. Binary search in C++: code and std::binary_search

    Binary search in C++: a loop that returns the index, the recursive version, std::binary_search and lower_bound, and why the range must be sorted.

    §2.107 min readC++17

    written for C++17

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

    §2.116 min readC++17

    written for C++17

  4. C++ cout: how to print with std::cout

    C++ 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.

    §2.127 min readC++17

    written for C++17

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

    §2.137 min readC++17

    written for C++17

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

    §2.147 min readC++11

    written for C++11

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

    §2.157 min readC++17

    written for C++17

  8. C++ iterator: how begin() and end() work

    What a C++ iterator is and how to loop over a vector with one: begin() and end(), std::next and std::distance, erasing in a loop, and when one goes stale.

    §2.167 min readC++17

    written for C++17

  9. C++ linked list: write one, then use std::list

    How to write a C++ linked list that frees its nodes, insert and delete nodes, then use std::list and std::forward_list, and when a vector is the better pick.

    §2.178 min readC++17

    written for C++17

  10. C++ pair: what std::pair is and how to use it

    C++ pair explained: make a std::pair, read first and second, compare two pairs, sort a vector of pairs and loop over the pairs in a std::map.

    §2.187 min readC++17

    written for C++17