Search⌘ K

Direct on the Container

Explore how to simplify STL algorithms by working directly on containers using C++20 ranges. Learn to create direct views on container keys and values, enabling lazy evaluation and cleaner code that mimics Python-like functionality.

We'll cover the following...

The algorithms of the Standard Template Library (STL) are sometimes a little inconvenient. They need both begin and end iterators. This is often more than you want to write.

C++
#include <numeric>
#include <iostream>
#include <vector>
int main() {
std::vector<int> myVec{1, 2, 3, 4, 5, 6, 7, 8, 9};
auto res = std::accumulate(std::begin(myVec), std::end(myVec), 0);
// std::accumulate(myVec, 0);
std::cout << res << '\n'; // 45
}

std::accumulate computes the sum of the elements of the ...