-
[c++] std::accumulate 함수 vector sumProgramming/c++ 2022. 7. 16. 20:07728x90
- accumulate 함수
STL container의 원소의 합을 모두 구하고 싶을때 사용하면 유용한 함수.
1. accumulate 함수 헤더 파일
#include <numeric>
2. 함수 원형
std::accumulate(const _INIT , const _INIT , int)
컨테이너의 iterator(반복자) , sum의 초기값을 Agument(인자)로 설정합니다.
3. return 값
컨테이너 원소들의 합을 모두 구한 int형으로 반환
4. 예제
#include <iostream> #include <numeric> #include <vector> int main() { std::vector<int> nVec = {1,3,5,87,15}; int nSum = std::accumulate(nVec.begin(), nVec.end(), 0); std::cout << nSum; return 0; }
accumulate 실행 결과 728x90'Programming > c++' 카테고리의 다른 글
[c++] lower_bound, upper_bound 함수 (0) 2022.09.15 [c++] unique 함수 (0) 2022.08.01 [c++] Visual Studio Code c++ 설정 (0) 2022.06.26 [c++] isdigit() 함수 (0) 2022.05.28 Jsoncpp 적용하기 (Visual Studio 2017) 방법1 (0) 2021.09.14