-
-
Save suminb/1306709 to your computer and use it in GitHub Desktop.
My First C++0x (C++11) Program
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 <iterator> | |
#include <list> | |
using namespace std; | |
list<int> map(int (*f)(int x), list<int>* l) { | |
list<int> result; | |
list<int>::iterator it = l->begin(); | |
while(it != l->end()) { | |
result.push_back(f(*it++)); | |
} | |
return result; | |
} | |
/* | |
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; | |
list<int> p = map([](int x) { return x * x; }, &l); | |
for(list<int>::iterator it=p.begin(); it!=p.end(); ++it) { | |
cout << *it << ' '; | |
} | |
cout << endl; | |
return 0; | |
} |
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
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