Created
February 26, 2019 09:47
-
-
Save hmenn/cd805791aea92b7abc020f7b4a6b3964 to your computer and use it in GitHub Desktop.
CXX11 Accumulate function examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <numeric> | |
#include <vector> | |
int main() { | |
std::vector<int> v{1,2,3,4,5,6,7,8,9,10}; | |
auto dash_fold = [](std::string a, int b){ | |
return a+ "-"+ std::to_string(b); | |
}; | |
int sum = std::accumulate(v.begin(), v.end(),0); | |
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()); | |
std::string dashFolded = std::accumulate(std::next(v.begin()), v.end(), | |
std::to_string(v[0]), dash_fold); | |
std::cout<<"Sum:"<<sum<<std::endl; | |
std::cout<<"Product:"<<product<<std::endl; | |
std::cout<<"Dash Folded:"<< dashFolded<<std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment