filtering items in a container

Discussion in 'C++' started by Anonymous, Oct 27, 2007.

  1. Anonymous

    Anonymous 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
     
    Anonymous, Oct 27, 2007
    #1
    1. Advertising

  2. On 2007-10-27 10:19, Anonymous wrote:
    > 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().

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 27, 2007
    #2
    1. Advertising

  3. Anonymous wrote:
    > 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
     
    =?UTF-8?B?SnVyYWogSXZhbsSNacSH?=, Oct 27, 2007
    #3
  4. Anonymous

    Anonymous Guest

    Juraj IvanÄić wrote:
    > Anonymous wrote:


    >> 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


    Wow, this is EXACTLY what I'm looking for. The BOOST library is quickly
    becoming my favourite C++ library ! ;)
     
    Anonymous, Oct 27, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,531
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    539
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,962
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    826
    Daniel T.
    Feb 16, 2006
  5. JMnich

    Filtering/Reducing Menu-Items

    JMnich, Apr 3, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    327
    JMnich
    Apr 3, 2008
Loading...

Share This Page