Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Filtering a vector attribute with template UnaryPredicate
//Filtering a vector attribute with template UnaryPredicate and boost phoenix #include <iostream> #include <string> #include <vector> #include <algorithm> #include <boost/phoenix/phoenix.hpp> #include <boost/foreach.hpp> struct Parent{ int getAge() const{ return age; } int getYear() const{ return year; } std::string getName() const{ return name; } int age; int year; std::string name; }; struct ListParent{ void AddParent(Parent* iparent){ parents.push_back(iparent); } template<typename UnaryPredicate> std::vector<Parent*> Find(UnaryPredicate Predicate) { std::vector<Parent*> aVolatil = parents; std::vector<Parent*>::iterator aNewEnd = std::remove_if(aVolatil.begin(), aVolatil.end(), !Predicate); std::vector<Parent*> aListParentsFilteredFinal(aVolatil.begin(), aNewEnd); return aListParentsFilteredFinal; } std::vector<Parent*> parents; }; int main() { ListParent aListParents; Parent aParent1; aParent1.age=30; aParent1.year=1980; aParent1.name="parent1"; Parent aParent2; aParent2.age=20; aParent2.year=1990; aParent2.name="parent2"; Parent aParent3; aParent3.age=10; aParent3.year=2000; aParent3.name="parent3"; aListParents.AddParent(&aParent1); aListParents.AddParent(&aParent2); aListParents.AddParent(&aParent3); std::vector<Parent*> FoundParents = aListParents.Find((boost::phoenix::bind(&Parent::getYear, boost::phoenix::placeholders::arg1) == 1980)|| (boost::phoenix::bind(&Parent::age, boost::phoenix::placeholders::arg1) == 20)|| (boost::phoenix::bind(&Parent::getName, boost::phoenix::placeholders::arg1) == "parent3") ); BOOST_FOREACH(Parent* aParent, FoundParents) { std::cout << aParent->name << std::endl; } }
run
|
edit
|
history
|
help
0
003#
LP(Multi)
sdfg
Dij. Algo
不带头结点的单链表
shell sort
find duplicate in O(n)
Namespace
Mirror a binary tree
Stream2