• Advertisement
Sign in to follow this  

STL - Map question

This topic is 4371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can you have a map such as this?

std::map<sockaddr_in, long>


sockaddr_in is defined in the winsock2.h. I want to be able to take an incoming ipaddress/port and convert it into an index. However, I get errors doing that.

1>f:\vs8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' :
could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const sockaddr_in'


Any ideas on how to fix this?

Share this post


Link to post
Share on other sites
Advertisement
Give it a compare function/functor.

E.g.

struct CompareIP
{
bool operator()( const sockaddr_in &one, const sockaddr_in &two ) const
{
if( one.host < two.host )
{
return true;
}
return one.port < two.port;
}
};

std::map<sockaddr_in, long, CompareIP>






[Edited by - Fruny on March 6, 2006 12:54:45 AM]

Share this post


Link to post
Share on other sites
Usually you overload the '<' operator for your class. The reason is because a std::map is sorted into a tree internally, and if there is no way of comparing one object to another the map will not know how to sort them.

If you want a cheap way of doing it, it is possible to map a class pointer to something though, since it is very easy to compare pointer addresses (they are just numbers afterall). So you could have this:

std::map<Foo*, Bar>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement