c++의 std::accumulate 연속 작업,누적 누적 등
2208 단어 c/c++
#include
#include
#include
#include
#include
using namespace std;
//http://www.cplusplus.com/reference/numeric/accumulate/
// accumulate example
//100 10 20 30=>100+10*2+20*2+30*2=220
int myfunction(int x, int y)
{
int ret = x + 2 * y;
return ret;
}
struct myclass {
//100 10 20 30=>100+10*3+20*3+30*3=280
int operator()(int x, int y) {
int ret = x + 3 * y;
return ret;
}
} myobject;
int test_accumulate002() {
int init = 100;
int numbers[] = { 10, 20, 30 };
std::cout << "using default accumulate: ";
std::cout << std::accumulate(numbers, numbers + 3, init);// init=100
std::cout << '
';
std::cout << "using functional's minus: ";
std::cout << std::accumulate(numbers, numbers + 3, init, std::minus());// init=100
std::cout << '
';
std::cout << "using custom function: ";
std::cout << std::accumulate(numbers, numbers + 3, init, myfunction);// init=100 myfunction
std::cout << '
';
std::cout << "using custom object: ";
std::cout << std::accumulate(numbers, numbers + 3, init, myobject);// init=100 myobject ()
std::cout << '
';
return 0;
}
//http://en.cppreference.com/w/cpp/algorithm/accumulate
//c++ std::accumulate
//template T accumulate(InputIterator first, InputIterator last, T init)
// , , binary_op。
int main(int argc, char const *argv[])
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
size_t size = sizeof(arr) / sizeof(arr[0]);
cout <()) << endl;//
vector v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::string s = std::accumulate(std::next(v.begin()), v.end(),
std::to_string(v[0]), // start with first element 。
[](std::string a, int b) {
return a + '-' + std::to_string(b);
});
cout << " =" << s << endl;
test_accumulate002();
system("pause");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
3. 배열 의 성명 및 초기 화 (test 1. java)먼저 배열 의 신청, 배열 의 초기 화, 배열 의 복사 등 을 배 웠 습 니 다.배열 에 대해 저 는 C \ C + + 의 배열 과 큰 차이 가 없다 고 생각 합 니 다. 그러나 JAVA 에서 JAVA 는 대상 을 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.