SoFunction
Updated on 2025-03-03

Detailed introduction and specific cases of C++ accumulate function

1. A brief introduction to the functions

Accumulate isnumericA function in the library is mainly used to sum elements in the specified range, but also specifies some other operations on its own, such as multiplication and division of all elements in the range.

The corresponding header file needs to be introduced before use.

#include <numeric>
  • There are four parameters in total, of which the first three are required and the fourth is non-essential.
  • If the fourth parameter is not specified, the elements in the range are accumulated by default.
accumulate(Start iterator, End iterator, Initial value, Custom operation functions)

2. Specific usage scenarios

1. Calculate the sum of all elements in the array

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;
using namespace std;

int main() {
    vector&lt;int&gt; arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate((), (), 0); // Initial value 0 + (1 + 2 + 3 + 4 +... + 10)    cout &lt;&lt; sum &lt;&lt; endl;	// Output 55    return 0;
}

2. Calculate the product of all elements in the array

The fourth parameter needs to be specified, and the multiplication function is used here. multiplies<type>(), type is selected according to the type of element.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;

using namespace std;

int main() {
    vector&lt;int&gt; arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate((), (), 1, multiplies&lt;int&gt;()); // Initial value 1 * (1 * 2 * 3 * 4 *... * 10)    cout &lt;&lt; sum &lt;&lt; endl;	// Output 3628800    return 0;
}

3. Calculate the sum after each element in the array is multiplied by 3.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;

using namespace std;

int fun(int acc, int num) {
    return acc + num * 3;     // Calculate each element in the array multiplied by 3}

int main() {
    vector&lt;int&gt; arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate((), (), 0, fun);
    cout &lt;&lt; sum &lt;&lt; endl;	// Output 165    return 0;
}

4. Calculate the sum after minus 3 of each element in the array

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;

using namespace std;

int fun(int acc, int num) {
    return acc + (num - 3) ;     // Calculate the sum after minus 3 of each element in the array}

int main() {
    vector&lt;int&gt; arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate((), (), 0, fun);
    cout &lt;&lt; sum &lt;&lt; endl;    // Output 25    return 0;
}

5. Calculate the average scores of students in the class

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;

using namespace std;

struct Student {
    string name;
    int score;
    Student() {};   // No parameter constructor    Student(string name, int score) : name(name), score(score) {};  // Parameter constructor};

int fun(int acc, Student b) {
    return a + ;
}

int main() {
    vector&lt;Student&gt; arr;
    arr.emplace_back("Alice", 82);
    arr.emplace_back("Bob", 91);
    arr.emplace_back("Lucy", 85);
    arr.emplace_back("Anna", 60);
    arr.emplace_back("June", 73);
    int avg_score = accumulate((), (), 0, fun) / ();	// Total score/number of students    cout &lt;&lt; avg_score &lt;&lt; endl;
    return 0;
}

6. Splice strings

It can also be used between strings in C+++, that is, splicing two strings.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;

using namespace std;

int main() {
    vector&lt;string&gt; words{"this ", "is ", "a ", "sentence!"};
    string init, res;
    res = accumulate((), (), init);    //Connection string    cout &lt;&lt; res &lt;&lt; endl;    // this is a sentence!
    return 0;
}

This is the article about the detailed introduction and specific cases of C++ accumulated functions. For more related C++ accumulated functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!