std::valarray::sum(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

std::valarray::sum β€” okay so this is literally std::valarray::sum, bestie. Welcome to the std::valarray::sum(3) page from the C++ Standard Libary manual β€” yes, that's the manual name, and yes, we're doing this. This whole page is about one member function with "add up the elements" energy. πŸ’…


πŸ’… Synopsis πŸ’…

T sum() const;

It computes the sum of the elements. You can use this function only if operator+= is defined for type T. If the std::valarray is empty, the behavior is undefined β€” like, genuinely undefined, not a cute little edge case. The order in which the elements are processed by this function is unspecified. 🫢

🌸 Parameters 🌸

(none) β€” literally no parameters, bestie. Not a single one. πŸŽ€

πŸŽ€ Return value πŸŽ€

The sum of the elements. That's the whole return value. Iconic. ⭐

πŸ¦‹ Example πŸ¦‹

Here's the code, and yes, run it:

#include <iostream>
#include <valarray>

int main()
{
    std::valarray<int> a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::cout << a.sum() << '\n';
}

⭐ Output ⭐

55

πŸ”₯ See also πŸ”₯

  • apply β€” applies a function to every element of a valarray (public member function)
  • accumulate β€” sums up or folds a range of elements (function template)

πŸ’– cute little sign-off: this page is from cppreference.com, dated 2024.06.10, and it's the std::valarray::sum(3) entry. slay, bye bestie 🫢✨

πŸ“¦ where this page comes from