이 튜토리얼에서는 C++에서 STL을 사용하여 벡터 요소의 합을 찾는 방법을 이해하는 프로그램에 대해 설명합니다.
주어진 벡터 요소의 합을 찾으려면 STL 라이브러리의 누적() 메서드를 사용합니다.
예
#include <bits/stdc++.h>
using namespace std;
int main(){
//defining the vector
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
//calculating sum of the elements
cout << "Sum = "<< accumulate(a.begin(), a.end(), 0);
return 0;
} 출력
Vector: 1 45 54 71 76 12 Sum = 259