Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Filtering a vector attribute with template UnaryPredicate
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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; } }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1.43 sec, absolute running time: 0.15 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 1.58 sec
edit mode
|
history
|
discussion
parent1 parent2 parent3