Hot to get iterator to specific map key?

S

Steve555

I have a map with integer keys.

How do I loop through with an iterator so that I start with an iter
that corresponds to a key >= x?
(i.e. in the loop I only want to operate on elements with a key >= x)

myMapType::iterator iter, startIter = iter starting with x ???;
for(iter = startIter; iter != myMap.end(); ++iter)
{
....
}

Is this possible or do I just have to start from myMap.begin() and
check each key?
I can't use advance() as the keys aren't contiguous

Thanks
Steve
 
L

Leandro Melo

I have a map with integer keys.

How do I loop through with an iterator so that I start with an iter
that corresponds to a key >= x?
(i.e. in the loop I only want to operate on elements with a key >= x)

myMapType::iterator iter,   startIter = iter starting with x ???;
for(iter = startIter; iter != myMap.end(); ++iter)
{
...

}

Is this possible or do I just have to start from myMap.begin() and
check each key?
I can't use advance() as the keys aren't contiguous


The following would give you an iterator to the first element whose
key is not less than x. If no such element exists you'll get a past-
the-end iterator.

myMapType::iterator iter = myMap.lower_bound(x);
 
S

Steve555

The following would give you an iterator to the first element whose
key is not less than x. If no such element exists you'll get a past-
the-end iterator.

myMapType::iterator iter = myMap.lower_bound(x);

Thanks for the reply. Of course, classic case of not seeing the wood
for the trees.

Steve
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top