-
-
Save dahlia/1306699 to your computer and use it in GitHub Desktop.
My First C++0x (C++11) Program
This file contains 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 <iterator> | |
#include <list> | |
using namespace std; | |
template<typename Iter> | |
iterator_traits<Iter>::value_type | |
reduce(function<iterator_traits<Iter>::value_type (iterator_traits<Iter>::value_type, iterator_traits<Iter>::value_type)> f, | |
Iter begin, Iter end, | |
iterator_traits<Iter>::value_type init) { | |
Iter it(begin); | |
iterator_traits<Iter>::value_type result = f(init, *it++); | |
while(it != l->end()) { | |
result = f(result, *it++); | |
} | |
return result; | |
} | |
int main() { | |
list<int> l; | |
l.push_back(1); | |
l.push_back(2); | |
l.push_back(3); | |
l.push_back(4); | |
cout << reduce([](int x, int y) { return x + y; }, l.begin(), l.end(), 0) | |
<< endl; | |
return 0; | |
} |
This file contains 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
print reduce(lambda x, y: x + y, (1, 2, 3, 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment