The following value in std :: map

I have it std::map<std::string, float>, so I can quickly search for float values ​​based on names, but now I have a situation where I need to find the next float value in the list, as if it were sorted numerically. As far as I know, I can’t just use an iterator, since the map is sorted by keywords. Therefore, if my set contains:

std::pair<"One", 1.0>
std::pair<"Two", 2.0>
std::pair<"Three", 3.0>
std::pair<"Four", 4.0>
std::pair<"Five", 5.0>

If I want to find the next value after “Three,” what would be the most efficient way to do this? The expected result in this case will be 4.0. I can iterate over the entire list and save the maximum value along with the current next value. Is this the best way?

+3
source share
2 answers
+6

Boost, ( , ). , , :

set<float>::const_iterator found = set.find(map["Three"]);
++found;
assert(*found == 4.0);
+1

Source: https://habr.com/ru/post/1729259/


All Articles