std::equal_range Behaviour

D

duey

Thanks in advance;

What I'm trying to do is find all the elements in the map that have a
certain value. The question I have is can you do this when the value
you are trying to find is not in the key of the map? The code seems to
work except that when I iterate through the result I end up getting
values that don't match the criteria.

Is there another algorithm that I should use instead? For instance,
can I use find_if in a while loop or something like that?

Here is a snippet of the code:

class ChannelFields
{
public:
long lngPortId;
}

class CompChannelPortId
{
public:
CompChannelPortId() {}
bool operator() ( const std::pair<long, ChannelFields>& lhs, const
std::pair<long, ChannelFields>& rhs ) const
{
return( lhs.second.lngPortId < rhs.second.lngPortId )
}
};

class ChannelDbMap : std::map<long, ChannelFields>
{
public:
void
refreshMap( long id )
{
std::pair<long, ChannelFields> compChannel;
compChannel.second.lngPortId = id;
std::pair<iterator, iterator> range
= std::equal_range( begin(), end(), compChannel,
CompChannelPortId() );
}
}
 
G

google

Is there another algorithm that I should use instead?

Use std::map::equal_range member function:

std::map<long, ChannelFields> m;
typedef std::map<long, ChannelFields>::iterator iterator;

std::pair<iterator, iterator> range = m.equal_range(compChannel);
 
D

duey

Use std::map::equal_range member function:

std::map<long, ChannelFields> m;
typedef std::map<long, ChannelFields>::iterator iterator;

std::pair<iterator, iterator> range = m.equal_range(compChannel);

Thanks for the reply.

Isn't the member function just going to return elements where the key
is the same?

Kevin
 
B

Bo Persson

duey said:
Thanks in advance;

What I'm trying to do is find all the elements in the map that have
a certain value. The question I have is can you do this when the
value you are trying to find is not in the key of the map? The code
seems to work except that when I iterate through the result I end
up getting values that don't match the criteria.

True, equal_range assumes that the sequence is ordered according to
the search criteria. For a map that means the key, unless there is
some very strong relation between the key and the data.
Is there another algorithm that I should use instead? For instance,
can I use find_if in a while loop or something like that?

Probably. Or perhaps a for_each with a conditional in the operation.


Bo Persson
 
A

Andrew Koenig

What I'm trying to do is find all the elements in the map that have a
certain value.

Yes, but not using equal_range. For one thing, there is no reason to
believe that the elements with a given value form a range -- they might be
anywhere in the map. For another, maps are geared toward finding an element
with a given key quickly; they do not know anything special about the
element values.

So finding elements with a particular value in a map is much like finding
elements with a particular value in any other data structure.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top