1. A brief introduction to the functions
Accumulate isnumeric
A 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 <iostream> #include <vector> #include <numeric> using namespace std; int main() { vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = accumulate((), (), 0); // Initial value 0 + (1 + 2 + 3 + 4 +... + 10) cout << sum << 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 <iostream> #include <vector> #include <numeric> using namespace std; int main() { vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = accumulate((), (), 1, multiplies<int>()); // Initial value 1 * (1 * 2 * 3 * 4 *... * 10) cout << sum << endl; // Output 3628800 return 0; }
3. Calculate the sum after each element in the array is multiplied by 3.
#include <iostream> #include <vector> #include <numeric> 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<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = accumulate((), (), 0, fun); cout << sum << endl; // Output 165 return 0; }
4. Calculate the sum after minus 3 of each element in the array
#include <iostream> #include <vector> #include <numeric> 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<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = accumulate((), (), 0, fun); cout << sum << endl; // Output 25 return 0; }
5. Calculate the average scores of students in the class
#include <iostream> #include <vector> #include <numeric> 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<Student> 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 << avg_score << endl; return 0; }
6. Splice strings
It can also be used between strings in C++
+
, that is, splicing two strings.
#include <iostream> #include <vector> #include <numeric> using namespace std; int main() { vector<string> words{"this ", "is ", "a ", "sentence!"}; string init, res; res = accumulate((), (), init); //Connection string cout << res << 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!