boost bind expression not doing what I expect

S

Stone Free

#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;
}
 
S

SG

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

Peter Nimmo

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

Peter Nimmo

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

Peter Nimmo

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
 
S

Stone Free

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
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top