Skip to content

Instantly share code, notes, and snippets.

@gvnwltrs
Last active January 8, 2021 21:28
Show Gist options
  • Save gvnwltrs/bc97711c650a4756d6714160d4a7c678 to your computer and use it in GitHub Desktop.
Save gvnwltrs/bc97711c650a4756d6714160d4a7c678 to your computer and use it in GitHub Desktop.
Adding #CPP
// adding up members of a collection
// accumulate(); uses the '+' operator to add up all members in a collection; sum tool; another template!
// can also use lambdas instead
vector<int> a = {1, 2, 3, 4, 5};
for (int i : a)
{
total += i;
}
total = accumulate(begin(a), end(a), 0); // much better!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment