filtering items in a container

A

Anonymous

I am trying to write a filtering algorithm without reinventing the
wheel. I basically want to select items in a container that have a
certain 'property' - i.e. satisfy a certain criteria - to be provided by
a predicate function.

In pseudocode, it would look something like this:

template <class T1 /*container type*/, typename T2/* type held in
container*/>
T1<T2> filter(const T1<T2>& source, PredicateFunctor func)
{
T1<T2> filtered_output ;
std::for_each(source.begin(), source.end(), func());
//somehow append those matching to variable filtered_output
return filtered_output ;
}

The idea is so that I could use such an algorithm to filter or 'select'
items that match certain criteria from an STL container - e.g. a vector
or map of objects.

if there is an existing STL algo that does that - or you can thing of a
better way of doing (implementing) this, please let me know
 
G

Guest

I am trying to write a filtering algorithm without reinventing the
wheel. I basically want to select items in a container that have a
certain 'property' - i.e. satisfy a certain criteria - to be provided by
a predicate function.

In pseudocode, it would look something like this:

template <class T1 /*container type*/, typename T2/* type held in
container*/>
T1<T2> filter(const T1<T2>& source, PredicateFunctor func)
{
T1<T2> filtered_output ;
std::for_each(source.begin(), source.end(), func());
//somehow append those matching to variable filtered_output
return filtered_output ;
}

The idea is so that I could use such an algorithm to filter or 'select'
items that match certain criteria from an STL container - e.g. a vector
or map of objects.

if there is an existing STL algo that does that - or you can thing of a
better way of doing (implementing) this, please let me know

Sounds like remove_copy_if().
 
G

Guest

Anonymous said:
I am trying to write a filtering algorithm without reinventing the
wheel. I basically want to select items in a container that have a
certain 'property' - i.e. satisfy a certain criteria - to be provided by
a predicate function.

In pseudocode, it would look something like this:

template <class T1 /*container type*/, typename T2/* type held in
container*/>
T1<T2> filter(const T1<T2>& source, PredicateFunctor func)
{
T1<T2> filtered_output ;
std::for_each(source.begin(), source.end(), func());
//somehow append those matching to variable filtered_output
return filtered_output ;
}

The idea is so that I could use such an algorithm to filter or 'select'
items that match certain criteria from an STL container - e.g. a vector
or map of objects.

if there is an existing STL algo that does that - or you can thing of a
better way of doing (implementing) this, please let me know

http://www.boost.org/libs/iterator/doc/filter_iterator.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top