ใน C++ สมัยใหม่ [11,14,…] vector ถูกเตรียมใช้งานด้วยวิธีต่อไปนี้
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