boost bind expression not doing what I expect

Discussion in 'C++' started by Stone Free, Aug 19, 2009.

  1. Stone Free

    Stone Free Guest

    #include <map>
    #include <set>
    #include <vector>
    #include <algorithm>
    #include <boost/bind.hpp>

    using namespace boost::bind;

    typedef int DWORD;
    typedef std::pair<std::string, bool> user_info;
    typedef std::map<DWORD, user_info> USER_MAP;
    typedef std::vector<DWORD> VEC_STAFF;

    int main(int argc, _char* argv[])
    {
    USER_MAP m_Users;
    VEC_STAFF Staff;

    ...
    ...

    VEC_STAFF::iterator it = std::partition(Staff.begin(), Staff.end(),
    (bind(&USER_MAP::find, m_Users, _1) != m_Users.end()));

    return 0;
    }


    What I wanted to code to do is that for every entry in the Staff
    vector it should check to see whether that user id was present in the
    map, and if so move those items to the front.

    I have an example using a functor that works as I hoped the above code
    would, but the bind version is obviously not doing what I think its
    doing.

    struct InUsersMap : public
    std::unary_function<USER_MAP::key_type,bool>
    {
    InUsersMap(USER_MAP & theMap, USER_MAP::const_iterator end)
    : m_theMap(theMap)
    {
    }
    inline bool operator()(CSecurePatients::USER_MAP::key_type& id) const
    {
    return m_theMap.find(id) != m_theMap.end();
    }
    private:
    const USER_MAP &m_theMap;
    }
     
    Stone Free, Aug 19, 2009
    #1
    1. Advertising

  2. Stone Free

    SG Guest

    On 19 Aug., 10:27, Stone Free wrote:
    > #include <map>
    > #include <set>
    > #include <vector>
    > #include <algorithm>
    > #include <boost/bind.hpp>
    >
    > using namespace boost::bind;
    >
    > typedef int DWORD;
    > typedef std::pair<std::string, bool> user_info;
    > typedef std::map<DWORD, user_info> USER_MAP;
    > typedef std::vector<DWORD> VEC_STAFF;
    >
    > int main(int argc, _char* argv[])
    > {
    >   USER_MAP m_Users;
    >   VEC_STAFF Staff;
    >
    >   ...
    >
    >   VEC_STAFF::iterator it =
    > std::partition( Staff.begin(), Staff.end(),
    > bind(&USER_MAP::find, m_Users, _1) != m_Users.end() );
    > ...
    > }
    >
    > What I wanted to code to do is that for every entry in the Staff
    > vector it should check to see whether that user id was present in the
    > map, and if so move those items to the front.
    >
    > [boost bind expression not doing what I expect] (subject)


    At first glance, it looks okay. But you're creating a functor that
    works on a *copy* of m_Users and compare an iterator of the copy with
    an iterator of the original map. Try using a pointer:

    bind(&USER_MAP::find, &m_Users, _1)
    ^
    Cheers!
    SG
     
    SG, Aug 19, 2009
    #2
    1. Advertising

  3. Stone Free

    Peter Nimmo Guest

    > At first glance, it looks okay. But you're creating a functor that
    > works on a *copy* of m_Users and compare an iterator of the copy with
    > an iterator of the original map. Try using a pointer:
    >
    >   bind(&USER_MAP::find, &m_Users, _1)
    >                         ^
    > Cheers!
    > SG

    Thanks that's sorted it

    A colleague of mine thought that the problem was that I was comparing
    the address of the bind expression against the end iterator and that I
    needed to use a nested bind and std::not_equal_to. However I couldn't
    get the syntax right.

    Your solution seems better.
     
    Peter Nimmo, Aug 19, 2009
    #3
  4. Stone Free

    Peter Nimmo Guest

    > At first glance, it looks okay. But you're creating a functor that
    > works on a *copy* of m_Users and compare an iterator of the copy with
    > an iterator of the original map. Try using a pointer:
    >
    >   bind(&USER_MAP::find, &m_Users, _1)
    >                         ^
    > Cheers!
    > SG- Hide quoted text -
    >
    > - Show quoted text -

    Thanks that sorted it.
     
    Peter Nimmo, Aug 20, 2009
    #4
  5. Stone Free

    Peter Nimmo Guest

    > At first glance, it looks okay. But you're creating a functor that
    > works on a *copy* of m_Users and compare an iterator of the copy with
    > an iterator of the original map. Try using a pointer:
    >
    >   bind(&USER_MAP::find, &m_Users, _1)

    Thanks that sorted it
     
    Peter Nimmo, Aug 20, 2009
    #5
  6. Stone Free

    Stone Free Guest

    > At first glance, it looks okay. But you're creating a functor that
    > works on a *copy* of m_Users and compare an iterator of the copy with
    > an iterator of the original map. Try using a pointer:
    >
    >   bind(&USER_MAP::find, &m_Users, _1)
    >                         ^
    > Cheers!
    > SG- Hide quoted text -

    Thanks, that sorted it
     
    Stone Free, Aug 20, 2009
    #6
    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. Toby Bradshaw
    Replies:
    6
    Views:
    1,757
    Kai-Uwe Bux
    Jun 2, 2006
  2. Replies:
    0
    Views:
    592
  3. Christopher
    Replies:
    1
    Views:
    814
    Yakov Gerlovin
    Oct 5, 2011
  4. Simon Strandgaard

    how to expect eof with expect+pty

    Simon Strandgaard, Dec 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    358
    Simon Strandgaard
    Dec 20, 2006
  5. Mark
    Replies:
    1
    Views:
    435
    Jeff Flinn
    Nov 25, 2012
Loading...

Share This Page