...

/

Direct on the Container

Direct on the Container

Visualize how to work directly on a container.

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.

Press + to interact
#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 ...