Skip to content

Instantly share code, notes, and snippets.

@suminb
Forked from dahlia/hello_c++0x.cpp
Created October 23, 2011 00:54
Show Gist options
  • Save suminb/1306709 to your computer and use it in GitHub Desktop.
Save suminb/1306709 to your computer and use it in GitHub Desktop.
My First C++0x (C++11) Program
#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;
}
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