Skip to content

Instantly share code, notes, and snippets.

@dahlia
Forked from suminb/hello_c++0x.cpp
Created October 23, 2011 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dahlia/1306699 to your computer and use it in GitHub Desktop.
Save dahlia/1306699 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;
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;
}
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