using std::find

Discussion in 'C++' started by brekehan, Mar 14, 2007.

  1. brekehan

    brekehan Guest

    If I have a class
    MyClass
    {
    ...bunch o data and methods
    int x;
    };

    and a stl container of MyClass objects

    Is there a way to use std::find to get all the elements whose member x
    is of om value?

    All the example on the net are of a collection of some built in type
    for simplicity, but do not show how to use a member of your own type
    as the value being sought.
     
    brekehan, Mar 14, 2007
    #1
    1. Advertising

  2. brekehan wrote:
    > If I have a class
    > MyClass
    > {
    > ...bunch o data and methods
    > int x;
    > };
    >
    > and a stl container of MyClass objects
    >
    > Is there a way to use std::find to get all the elements whose member x
    > is of om value?


    "om" value? What's "om" value? Sorry, English is not my mother
    tongue.

    > All the example on the net are of a collection of some built in type
    > for simplicity, but do not show how to use a member of your own type
    > as the value being sought.


    What does your favourite C++ book say? Don't you have a copy of
    Josuttis' "The C++ Standard Library"? You can define your own
    functor to compare the value of the 'x' member with the given value
    and then call

    std::find(container.begin(), container.end(), yourFunctor(42));

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 14, 2007
    #2
    1. Advertising

  3. brekehan

    red floyd Guest

    Victor Bazarov wrote:
    > brekehan wrote:
    >> If I have a class
    >> MyClass
    >> {
    >> ...bunch o data and methods
    >> int x;
    >> };
    >>
    >> and a stl container of MyClass objects
    >>
    >> Is there a way to use std::find to get all the elements whose member x
    >> is of om value?

    >
    > "om" value? What's "om" value? Sorry, English is not my mother
    > tongue.
    >
    >> All the example on the net are of a collection of some built in type
    >> for simplicity, but do not show how to use a member of your own type
    >> as the value being sought.

    >
    > What does your favourite C++ book say? Don't you have a copy of
    > Josuttis' "The C++ Standard Library"? You can define your own
    > functor to compare the value of the 'x' member with the given value
    > and then call
    >
    > std::find(container.begin(), container.end(), yourFunctor(42));
    >
    > V


    I think he wants remove_copy_if() with negated functor?

    std::vector<MyClass> v;

    struct hasElement: public std::unary_function<MyClass, bool>
    {
    bool operator()(const MyClass& elem) const
    {
    return elem.x == x;
    }
    hasElement(int x_) : x(x_) { }
    private
    int x;
    };

    std::remove_copy_if(container.begin(), container.end(),
    std::back_inserter(v),
    std::not(hasElement(42))_;
     
    red floyd, Mar 14, 2007
    #3
  4. red floyd wrote:
    > Victor Bazarov wrote:
    >> brekehan wrote:
    >>> If I have a class
    >>> MyClass
    >>> {
    >>> ...bunch o data and methods
    >>> int x;
    >>> };
    >>>
    >>> and a stl container of MyClass objects
    >>>
    >>> Is there a way to use std::find to get all the elements whose
    >>> member x is of om value?

    >>
    >> "om" value? What's "om" value? Sorry, English is not my mother
    >> tongue.
    >>
    >>> All the example on the net are of a collection of some built in type
    >>> for simplicity, but do not show how to use a member of your own type
    >>> as the value being sought.

    >>
    >> What does your favourite C++ book say? Don't you have a copy of
    >> Josuttis' "The C++ Standard Library"? You can define your own
    >> functor to compare the value of the 'x' member with the given value
    >> and then call
    >>
    >> std::find(container.begin(), container.end(), yourFunctor(42));
    >>
    >> V

    >
    > I think he wants remove_copy_if() with negated functor?
    >
    > std::vector<MyClass> v;
    >
    > struct hasElement: public std::unary_function<MyClass, bool>
    > {
    > bool operator()(const MyClass& elem) const
    > {
    > return elem.x == x;
    > }
    > hasElement(int x_) : x(x_) { }
    > private
    > int x;
    > };
    >
    > std::remove_copy_if(container.begin(), container.end(),
    > std::back_inserter(v),
    > std::not(hasElement(42))_;


    I couldn't find the word "remove" in the original post. Only "get"
    and "find". For that 'find' is called in a 'while' loop and the
    beginning of the range should keep changing:

    it = c.begin();
    while (true) {
    it = std::find(it, c.end(), hasElement(42));
    if (it != c.end()) {
    // do something with 'it' or with '*it' or with it->x or...
    }
    else
    break;
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 14, 2007
    #4
  5. brekehan

    David Harmon Guest

    On 14 Mar 2007 10:48:40 -0700 in comp.lang.c++, "brekehan"
    <> wrote,
    >If I have a class
    >MyClass
    >{
    > ...bunch o data and methods
    > int x;
    >};
    >
    >and a stl container of MyClass objects
    >
    >Is there a way to use std::find to get all the elements whose member x
    >is of om value?


    I think you might end up using std::find_if() with a predicate that
    selects the values you want. The predicate could be a boost::lambda
    expression, or it could be hand crafted. Since find() and find_if()
    return just one thing, you could loop to find them all.

    Some random fragments:

    bool ifx(MyClass const & c, int x)
    { return c.x == x; }

    for (iterator_type it = container.begin(); it != container.end();
    it = find_if(it, container.end(), std::bind_2nd(ifx, om))) {
    if (it != container.end())
    cout << *it++;
    }
     
    David Harmon, Mar 14, 2007
    #5
  6. brekehan

    brekehan Guest

    On Mar 14, 12:53 pm, "Victor Bazarov" <> wrote:
    > brekehan wrote:
    > > If I have a class
    > > MyClass
    > > {
    > > ...bunch o data and methods
    > > int x;
    > > };

    >
    > > and a stl container of MyClass objects

    >
    > > Is there a way to use std::find to get all the elements whose member x
    > > is of om value?

    >
    > "om" value? What's "om" value? Sorry, English is not my mother
    > tongue.
    >
    > > All the example on the net are of a collection of some built in type
    > > for simplicity, but do not show how to use a member of your own type
    > > as the value being sought.

    >
    > What does your favourite C++ book say? Don't you have a copy of
    > Josuttis' "The C++ Standard Library"? You can define your own
    > functor to compare the value of the 'x' member with the given value
    > and then call
    >
    > std::find(container.begin(), container.end(), yourFunctor(42));
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask



    om = some. this keyboard sucks. Sorry. I also used to have three
    really good STL references, but the girlfriend got angry that I love
    the computer more than her and disposed of most of my office. I plan
    on getting some more, meantime I got a deadline to meet.

    I see that a few of the functions in <algorithm> or the container
    itself take the functor argument you mentioned, but if I understand
    correctly this is passed 1 argument of the type contained. remove_if
    would be awesome, because that is exatly what my goal is. Remove an
    element if it has a member (we'll call it TYPE m_type) of some type.
    However, I need to have two arguments...the element and the TYPE to
    compare it with, so my functor can say is this element of this type?
    Otherwise, I have to write a functor for every possible value of
    m_type and there is no telling how many there may be in the future.
     
    brekehan, Mar 14, 2007
    #6
  7. brekehan

    David Harmon Guest

    On 14 Mar 2007 11:35:52 -0700 in comp.lang.c++, "brekehan"
    <> wrote,
    >However, I need to have two arguments...the element and the TYPE to
    >compare it with, so my functor can say is this element of this type?
    >Otherwise, I have to write a functor for every possible value of
    >m_type and there is no telling how many there may be in the future.


    Solving that part of the problem is the job of the std::bind* family.
    Or else something more modern and powerful like boost::bind.
     
    David Harmon, Mar 14, 2007
    #7
  8. brekehan

    Marcus Kwok Guest

    brekehan <> wrote:
    > If I have a class
    > MyClass
    > {
    > ...bunch o data and methods
    > int x;
    > };
    >
    > and a stl container of MyClass objects
    >
    > Is there a way to use std::find to get all the elements whose member x
    > is of om value?


    I believe std::find by default uses std::less<T>, which by default uses
    operator<. So, you can provide your own operator<, or explicitly
    provide a comparison functor to find().

    Then, one way would be to sort your container, then use std::lower_bound
    and std::upper_bound.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Mar 14, 2007
    #8
  9. brekehan

    red floyd Guest

    Victor Bazarov wrote:
    > red floyd wrote:
    >> Victor Bazarov wrote:
    >>> brekehan wrote:
    >>>> If I have a class
    >>>> MyClass
    >>>> {
    >>>> ...bunch o data and methods
    >>>> int x;
    >>>> };
    >>>>
    >>>> and a stl container of MyClass objects
    >>>>
    >>>> Is there a way to use std::find to get all the elements whose
    >>>> member x is of om value?
    >>> "om" value? What's "om" value? Sorry, English is not my mother
    >>> tongue.
    >>>
    >>>> All the example on the net are of a collection of some built in type
    >>>> for simplicity, but do not show how to use a member of your own type
    >>>> as the value being sought.
    >>> What does your favourite C++ book say? Don't you have a copy of
    >>> Josuttis' "The C++ Standard Library"? You can define your own
    >>> functor to compare the value of the 'x' member with the given value
    >>> and then call
    >>>
    >>> std::find(container.begin(), container.end(), yourFunctor(42));
    >>>
    >>> V

    >> I think he wants remove_copy_if() with negated functor?
    >>
    >> std::vector<MyClass> v;
    >>
    >> struct hasElement: public std::unary_function<MyClass, bool>
    >> {
    >> bool operator()(const MyClass& elem) const
    >> {
    >> return elem.x == x;
    >> }
    >> hasElement(int x_) : x(x_) { }
    >> private
    >> int x;
    >> };
    >>
    >> std::remove_copy_if(container.begin(), container.end(),
    >> std::back_inserter(v),
    >> std::not(hasElement(42))_;

    >
    > I couldn't find the word "remove" in the original post. Only "get"
    > and "find". For that 'find' is called in a 'while' loop and the
    > beginning of the range should keep changing:
    >
    > it = c.begin();
    > while (true) {
    > it = std::find(it, c.end(), hasElement(42));
    > if (it != c.end()) {
    > // do something with 'it' or with '*it' or with it->x or...
    > }
    > else
    > break;
    > }


    I thought remove_copy_if doesn't modify the original container. The
    construct I gave should copy all elements with x == 42.
     
    red floyd, Mar 14, 2007
    #9
    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. Peter Jansson
    Replies:
    5
    Views:
    6,356
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    377
    Jim Langston
    Jul 7, 2005
  3. Jeffrey Walton
    Replies:
    10
    Views:
    956
    Mathias Gaunard
    Nov 26, 2006
  4. Anonymous
    Replies:
    4
    Views:
    394
    Klaas Vantournhout
    Oct 17, 2007
  5. petry

    std::string::find vs std::find

    petry, Jul 5, 2009, in forum: C Programming
    Replies:
    1
    Views:
    354
    petry
    Jul 5, 2009
Loading...

Share This Page