# [C++] STL Algorithm Problem

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

## 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 on other sites
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 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!

1. 1
2. 2
3. 3
4. 4
JoeJ
12
5. 5
Rutin
11

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004598

×