Skip to content

Instantly share code, notes, and snippets.

@kennethho
Created September 6, 2012 09:59
Show Gist options
  • Save kennethho/3654186 to your computer and use it in GitHub Desktop.
Save kennethho/3654186 to your computer and use it in GitHub Desktop.
// $ g++ -std=c++0x filter.cpp
#include <iterator>
#include <algorithm>
template <class Sequence, class Predicate>
Sequence filter(const Sequence& seq, Predicate pred)
{
Sequence result;
std::copy_if(begin(seq), end(seq), back_inserter(result), pred);
return result;
}
#include <string>
#include <list>
using namespace std;
int main()
{
list<string> l = {"Hello ", "", "world"};
l = filter(l, [](const string& s) { return !s.empty(); });
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment