liveBook · Manning

C++ standard library

As explained in 100 C++ Mistakes and How to Avoid Them

The Standard Template Library is the C++ standard library collection of generic containers, algorithms, iterators, and related components.

The Standard Template Library (STL) provides generic containers and algorithms that replace repetitive, error-prone searching and utility code with reusable components. section 3.0 section 3.4

A typical mistake is writing indexed loops or custom sum and find functions when library algorithms already express the task more clearly and with fewer opportunities for bugs. section 3.1 section 3.4

Search with an STL algorithm

Use std::find with an iterator, then test it against values.end(). An if statement initializer keeps the iterator scoped to the decision that uses it. section 3.1

Listing 3.9 Two library functions that need not be written
int main() {
    std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::cout << std::accumulate(v.begin(), v.end(), 0) << '\n'; #1
    auto it = std::find(v.begin(), v.end(), 4);                  #2
    if (it != v.end())
        std::cout << "the key 4 was found\n";
    else
        std::cout << "the key 4 was not found\n";
}

Compare library implementations of accumulation and searching with custom functions that add unnecessary housekeeping code. section 3.4

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains searches and common algorithms through examples, but stops short of detailing STL internals or broad component coverage.
Examples
Demonstrates vector searches, std::find, std::accumulate, and custom sum and find implementations.
What it assumes
Assumes basic C++ containers and loops, while teaching how iterators and standard algorithms replace manual traversal.
What you can do after
Apply STL algorithms to search and aggregate containers while limiting temporary variables and handwritten utility code.
How it connects
Connects containers, algorithms, iterators, if statement initializers, auto, and uniform initialization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.