[C++] STL Algorithm Problem

Started by
1 comment, last by Fox32 14 years, 1 month ago
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]
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
[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!

This topic is closed to new replies.

Advertisement