आधुनिक C++ [11,14,…] में एक वेक्टर को निम्न तरीके से प्रारंभ किया जाता है
std::vector<int> vec = {1,2,3}; एल्गोरिदम
Begin Initialize the vector v. Using accumulate, sum up all the elements of the vector v is done. Print the result. 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