주어진 범위의 데이터를 인자로 넘겨진 계산식으로 계산해 반환한다.
first | 요소 범위의 시작 |
last | 요소 범위의 끝 |
init | 초기 값 |
op | 적용될 한 쌍의 작업 함수 객체 (시그니처: Ret func(const Type1 &a, const Type2 &b); |
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = std::accumulate(v.begin(), v.end(), 0);
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
auto dash_fold = [](std::string a, int b) {
return std::move(a) + '-' + std::to_string(b);
};
std::string s = std::accumulate(std::next(v.begin()), v.end(),
std::to_string(v[0]), // start with first element
dash_fold);
// Right fold using reverse iterators
std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(),
std::to_string(v.back()), // start with last element
dash_fold);
std::cout << "sum: " << sum << '\n'
<< "product: " << product << '\n'
<< "dash-separated string: " << s << '\n'
<< "dash-separated string (right-folded): " << rs << '\n';
}
Output:
sum: 55
product: 3628800
dash-separated string: 1-2-3-4-5-6-7-8-9-10
dash-separated string (right-folded): 10-9-8-7-6-5-4-3-2-1
https://en.cppreference.com/w/cpp/algorithm/accumulate
std::accumulate - cppreference.com
(1) template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init ); (until C++20) template< class InputIt, class T > constexpr T accumulate( InputIt first, InputIt last, T init ); (since C++20) (2) template< class InputIt, class T,
en.cppreference.com
https://en.cppreference.com/w/cpp/utility/functional/plus
std::plus - cppreference.com
template< class T > struct plus; (until C++14) template< class T = void > struct plus; (since C++14) Function object for performing addition. Effectively calls operator+ on two instances of type T. [edit] Specializations The standard library provides a spe
en.cppreference.com
https://en.cppreference.com/w/cpp/utility/functional/minus
std::minus - cppreference.com
template< class T > struct minus; (until C++14) template< class T = void > struct minus; (since C++14) Function object for performing subtraction. Effectively calls operator- on two instances of type T. [edit] Specializations The standard library provides
en.cppreference.com
https://en.cppreference.com/w/cpp/utility/functional/multiplies
std::multiplies - cppreference.com
template< class T > struct multiplies; (until C++14) template< class T = void > struct multiplies; (since C++14) Function object for performing multiplication. Effectively calls operator* on two instances of type T. [edit] Specializations The standard libr
en.cppreference.com
https://en.cppreference.com/w/cpp/utility/functional/divides
std::divides - cppreference.com
template< class T > struct divides; (until C++14) template< class T = void > struct divides; (since C++14) Function object for performing division. Effectively calls operator/ on two instances of type T. [edit] Specializations The standard library provides
en.cppreference.com
https://en.cppreference.com/w/cpp/utility/functional/modulus
std::modulus - cppreference.com
template< class T > struct modulus; (until C++14) template< class T = void > struct modulus; (since C++14) Function object for computing remainders of divisions. Implements operator% for type T. [edit] Specializations The standard library provides a specia
en.cppreference.com
'프로그래밍 > Morden C++' 카테고리의 다른 글
std::map (0) | 2021.10.05 |
---|---|
std::clamp (0) | 2021.09.29 |
[C++17] std::optional (0) | 2021.07.17 |
std::splice (0) | 2021.07.13 |
std::advance (0) | 2021.07.13 |