• Advertisement
Sign in to follow this  

[C++] STL Algorithm Problem

This topic is 2910 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

Hello, i have a small STL algorithm problem. I have a sortet std::map<float, float>. Now I want to perform the folowing operation:
float position;
std::map<float, float> keys;

[...]

keys.insert(std::pair<float, float>(position, 0));
std::map<float, float>::iterator iterator = keys.find(position);
std::map<float, float>::iterator before = *(--iterator);
std::map<float, float>::iterator after = *(++(++iterator));
keys.erase(--iterator);


Operation: Search the element that would be before and after the element, if I would add it to the map. The operation work, but is there a simpler way to do it (Without adding it to the map)? Thanks [Edited by - Fox32 on March 1, 2010 12:44:08 PM]

Share this post


Link to post
Share on other sites
Advertisement
have a look at lower_bound and upper_bound

also your coe is buggy if map is empty or if the item is found very neat the beginning or end

Share this post


Link to post
Share on other sites
[qoute]also your coe is buggy if map is empty or if the item is found very neat the beginning or end[/qoute]

I already check this before. lower_bound and upper_bound should be what I was searching for. Thanks!

Share this post


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

  • Advertisement