C++ वेक्टर के सभी तत्वों का योग std::accumulate विधि द्वारा बहुत आसानी से किया जा सकता है। इसे <संख्यात्मक> शीर्षलेख में परिभाषित किया गया है। यह वेक्टर में निर्दिष्ट सभी मानों को निर्दिष्ट योग में जमा करता है।
एल्गोरिदम
Begin Declare v of vector type. Initialize some values into v vector in array pattern. Print “Sum of all the elements are:”. Call accumulate(v.begin(),v.end(),0) to calculate the sum of all values of v vector. Print the result of sum. End.
उदाहरण कोड
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
vector<int> v = {2,7,6,10};
cout<<"Sum of all the elements are:"<<endl;
cout<<accumulate(v.begin(),v.end(),0);
} आउटपुट
Sum of all the elements are: 25