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
