Need help with STL.

Discussion in 'C++' started by Jason Heyes, Sep 9, 2004.

  1. Jason Heyes

    Jason Heyes Guest

    How do I rewrite the following code so that I am taking advantage of the
    STL? The purpose of the code is to determine whether a vector contains any
    objects of MyClass that are "true" (i.e., MyClass::is_true() returns true).

    bool found = false;
    vector<MyClass>::size_type i;
    for (i=0; i < objects.size(); i++)
    {
    MyClass object = objects;
    if (object.is_true())
    {
    found = true;
    break;
    }
    }

    Any help is very appreciated.
     
    Jason Heyes, Sep 9, 2004
    #1
    1. Advertising

  2. Jason Heyes

    Unforgiven Guest

    "Jason Heyes" <> wrote in message
    news:414077c5$0$2739$...
    > How do I rewrite the following code so that I am taking advantage of the
    > STL? The purpose of the code is to determine whether a vector contains any
    > objects of MyClass that are "true" (i.e., MyClass::is_true() returns
    > true).
    >
    > bool found = false;
    > vector<MyClass>::size_type i;
    > for (i=0; i < objects.size(); i++)
    > {
    > MyClass object = objects;
    > if (object.is_true())
    > {
    > found = true;
    > break;
    > }
    > }
    >
    > Any help is very appreciated.


    You're using vectors, so you're already taking advantage of the STL. Yes,
    it's possible to rewrite this code in a way that uses STL functionality even
    more, but you didn't indicate what exactly it is you want. Do you want to
    use iterators, std::find_if(), something else?

    Also, my spider sense is tingling on this one... this isn't homework by any
    chance, is it?

    --
    Unforgiven
     
    Unforgiven, Sep 9, 2004
    #2
    1. Advertising

  3. Jason Heyes

    Mike Wahler Guest

    "Jason Heyes" <> wrote in message
    news:414077c5$0$2739$...
    > How do I rewrite the following code so that I am taking advantage of the
    > STL? The purpose of the code is to determine whether a vector contains any
    > objects of MyClass that are "true" (i.e., MyClass::is_true() returns

    true).
    >
    > bool found = false;
    > vector<MyClass>::size_type i;
    > for (i=0; i < objects.size(); i++)
    > {
    > MyClass object = objects;
    > if (object.is_true())
    > {
    > found = true;
    > break;
    > }
    > }
    >
    > Any help is very appreciated.



    #include <algorithm>
    #include <iostream>
    #include <vector>

    class MyClass
    {
    public:
    MyClass(bool arg = false) : state(arg)
    {
    }

    bool is_true() const
    {
    return state;
    }

    private:
    bool state;
    };

    bool pred(const MyClass& mc)
    {
    return mc.is_true();
    }

    bool any_true(const std::vector<MyClass>& v)
    {
    return std::find_if(v.begin(), v.end(), pred) != v.end();
    }

    int main()
    {
    std::vector<MyClass> vec (10);
    std::cout << any_true(vec) << '\n';

    vec[5] = true;
    std::cout << any_true(vec) << '\n';

    return 0;
    }


    An alternative:
    Replaced member function 'is_true()' with 'operator bool()',
    uses 'std::find()' instead of 'std::find_if()'. (We no longer
    need the predicate function 'pred()').

    #include <algorithm>
    #include <iostream>
    #include <vector>

    class MyClass
    {
    public:
    MyClass(bool arg = false) : state(arg)
    {
    }

    operator bool () const
    {
    return state;
    }

    private:
    bool state;
    };


    bool any_true(const std::vector<MyClass>& v)
    {
    return std::find(v.begin(), v.end(), true) != v.end();
    }

    int main()
    {
    std::vector<MyClass> vec (10);
    std::cout << any_true(vec) << '\n';
    vec[5] = true;
    std::cout << any_true(vec) << '\n';

    return 0;
    }

    HTH,
    -Mike
     
    Mike Wahler, Sep 9, 2004
    #3
  4. Jason Heyes

    Daniel T. Guest

    "Jason Heyes" <> wrote:

    > How do I rewrite the following code so that I am taking advantage of the
    > STL? The purpose of the code is to determine whether a vector contains any
    > objects of MyClass that are "true" (i.e., MyClass::is_true() returns true).
    >
    > bool found = false;
    > vector<MyClass>::size_type i;
    > for (i=0; i < objects.size(); i++)
    > {
    > MyClass object = objects;
    > if (object.is_true())
    > {
    > found = true;
    > break;
    > }
    > }
    >
    > Any help is very appreciated.


    If you want to work it out for yourself, I suggest you look up find_if
    and mem_fun_ref. Otherwise...

    bool found =
    find_if( objects.begin(), objects.end(),
    mem_fun_ref( &MyClass:is_true ) ) != objects.end();
     
    Daniel T., Sep 9, 2004
    #4
  5. Jason Heyes

    Jason Heyes Guest

    "Unforgiven" <> wrote in message
    news:...
    > You're using vectors, so you're already taking advantage of the STL. Yes,
    > it's possible to rewrite this code in a way that uses STL functionality

    even
    > more, but you didn't indicate what exactly it is you want. Do you want to
    > use iterators, std::find_if(), something else?
    >
    > Also, my spider sense is tingling on this one... this isn't homework by

    any
    > chance, is it?
    >
    > --
    > Unforgiven


    No this isn't homework. The sort of STL I had in mind was std::find_if()
    used together with mem_fun_ref(). Is this how its done? Would you encourage
    writing this sort of code?

    bool found = find_if(objects.begin(), objects.end(),
    mem_fun_ref(MyClass::is_true)) != objects.end();

    Something I noticed was that mem_fun_ref_t<R,T>::eek:perator() takes a
    non-const reference only. Why isn't there a mem_fun_ref_t taking a const
    reference so that my code works when objects is const?
     
    Jason Heyes, Sep 9, 2004
    #5
  6. Jason Heyes

    Jason Heyes Guest

    "Daniel T." <> wrote in message
    news:p...
    > If you want to work it out for yourself, I suggest you look up find_if
    > and mem_fun_ref. Otherwise...
    >
    > bool found =
    > find_if( objects.begin(), objects.end(),
    > mem_fun_ref( &MyClass:is_true ) ) != objects.end();


    What happens if objects is const? The operator() function in mem_fun_ref_t
    only takes a non-const reference. Do I need to write my own template class
    called mem_fun_const_ref_t, say? Thanks.
     
    Jason Heyes, Sep 9, 2004
    #6
  7. Jason Heyes

    Jason Heyes Guest

    "Jason Heyes" <> wrote in message
    news:4140883e$0$2739$...
    > "Daniel T." <> wrote in message
    > news:p...
    > > If you want to work it out for yourself, I suggest you look up find_if
    > > and mem_fun_ref. Otherwise...
    > >
    > > bool found =
    > > find_if( objects.begin(), objects.end(),
    > > mem_fun_ref( &MyClass:is_true ) ) != objects.end();

    >
    > What happens if objects is const? The operator() function in mem_fun_ref_t
    > only takes a non-const reference. Do I need to write my own template class
    > called mem_fun_const_ref_t, say? Thanks.


    I also want something similar for the following code where the member
    function of MyClass being called takes a single argument.

    bool found = false;
    vector<MyClass>::size_type i;
    for (i=0; i < objects.size(); i++)
    {
    MyClass object = objects;
    if (object.is_equal(32))
    {
    found = true;
    break;
    }
    }

    Would this be the STL code to write?

    typedef mem_fun_ref1_t<bool, MyClass, int> func_t;
    binder2nd<func_t> pred = bind2nd(func_t(MyClass::is_equal), 32);
    bool found = find_if(objects.begin(), objects.end(), pred) !=
    objects.end();

    Thanks.
     
    Jason Heyes, Sep 9, 2004
    #7
  8. Jason Heyes

    Daniel T. Guest

    In article <41409518$0$30309$>,
    "Jason Heyes" <> wrote:

    > "Jason Heyes" <> wrote in message
    > news:4140883e$0$2739$...
    > > "Daniel T." <> wrote in message
    > > news:p...
    > > > If you want to work it out for yourself, I suggest you look up find_if
    > > > and mem_fun_ref. Otherwise...
    > > >
    > > > bool found =
    > > > find_if( objects.begin(), objects.end(),
    > > > mem_fun_ref( &MyClass:is_true ) ) != objects.end();

    > >
    > > What happens if objects is const? The operator() function in mem_fun_ref_t
    > > only takes a non-const reference. Do I need to write my own template class
    > > called mem_fun_const_ref_t, say? Thanks.

    >
    > I also want something similar for the following code where the member
    > function of MyClass being called takes a single argument.
    >
    > bool found = false;
    > vector<MyClass>::size_type i;
    > for (i=0; i < objects.size(); i++)
    > {
    > MyClass object = objects;
    > if (object.is_equal(32))
    > {
    > found = true;
    > break;
    > }
    > }
    >
    > Would this be the STL code to write?
    >
    > typedef mem_fun_ref1_t<bool, MyClass, int> func_t;
    > binder2nd<func_t> pred = bind2nd(func_t(MyClass::is_equal), 32);
    > bool found = find_if(objects.begin(), objects.end(), pred) !=
    > objects.end();


    I wouldn't bother with the seperate 'pred' object unless I was using it
    in more than one place:

    bool found =
    find_if( objects.begin(), objects.end(),
    bind2nd( mem_fun_ref( &MyClass::is_equal ), 32 ) ) != objects.end();

    Or if you really want the pred:

    binder2nd< const_mem_fun1_ref_t< bool, MyClass, int> >
    MyClassIsEquals32 = bind2nd( mem_fun_ref( &MyClass::is_equal ), 32 );
    bool found =
    find_if( objects.begin(), objects.end(), MyClassIsEquals32 ) !=
    objects.end();
     
    Daniel T., Sep 9, 2004
    #8
  9. Jason Heyes

    Daniel T. Guest

    In article <4140883e$0$2739$>,
    "Jason Heyes" <> wrote:

    > "Daniel T." <> wrote in message
    > news:p...
    > > If you want to work it out for yourself, I suggest you look up find_if
    > > and mem_fun_ref. Otherwise...
    > >
    > > bool found =
    > > find_if( objects.begin(), objects.end(),
    > > mem_fun_ref( &MyClass:is_true ) ) != objects.end();

    >
    > What happens if objects is const?


    Then a const_mem_fun_ref_t is created instead of a mem_fun_ref_t, and
    the code works.
     
    Daniel T., Sep 9, 2004
    #9
  10. Jason Heyes

    Jason Heyes Guest

    "Daniel T." <> wrote in message
    news:p...
    > In article <4140883e$0$2739$>,
    > "Jason Heyes" <> wrote:
    >
    > > "Daniel T." <> wrote in message
    > > news:p...
    > > > If you want to work it out for yourself, I suggest you look up find_if
    > > > and mem_fun_ref. Otherwise...
    > > >
    > > > bool found =
    > > > find_if( objects.begin(), objects.end(),
    > > > mem_fun_ref( &MyClass:is_true ) ) != objects.end();

    > >
    > > What happens if objects is const?

    >
    > Then a const_mem_fun_ref_t is created instead of a mem_fun_ref_t, and
    > the code works.


    Is there a const_mem_fun_ref_t in the standard library or must I write my
    own?
     
    Jason Heyes, Sep 10, 2004
    #10
  11. Jason Heyes

    Daniel T. Guest

    In article <41410064$0$30724$>,
    "Jason Heyes" <> wrote:

    > "Daniel T." <> wrote in message
    > news:p...
    > > In article <4140883e$0$2739$>,
    > > "Jason Heyes" <> wrote:
    > >
    > > > "Daniel T." <> wrote in message
    > > > news:p...
    > > > > If you want to work it out for yourself, I suggest you look up find_if
    > > > > and mem_fun_ref. Otherwise...
    > > > >
    > > > > bool found =
    > > > > find_if( objects.begin(), objects.end(),
    > > > > mem_fun_ref( &MyClass:is_true ) ) != objects.end();
    > > >
    > > > What happens if objects is const?

    > >
    > > Then a const_mem_fun_ref_t is created instead of a mem_fun_ref_t, and
    > > the code works.

    >
    > Is there a const_mem_fun_ref_t in the standard library or must I write my
    > own?


    There is one in the stardard.
     
    Daniel T., Sep 10, 2004
    #11
    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. Allan Bruce

    To STL or not to STL

    Allan Bruce, Oct 16, 2003, in forum: C++
    Replies:
    41
    Views:
    1,088
    Christopher Benson-Manica
    Oct 17, 2003
  2. Replies:
    4
    Views:
    815
    Daniel T.
    Feb 16, 2006
  3. Replies:
    2
    Views:
    562
    klaus hoffmann
    Feb 22, 2006
  4. Replies:
    5
    Views:
    511
    Markus Schoder
    Apr 16, 2006
  5. Steve
    Replies:
    2
    Views:
    516
    Andre Kostur
    Nov 6, 2007
Loading...

Share This Page