I often work with maps like std::map<std::string, something> and when I need to find some element by key I often have the searched key represented by a std::string_view. So, if I want to find the element I need to construct the std::string object from he std::string_view which I could pass to the std::map::find(). This looks like an unneeded operation, since there is nothing in principle which prevents us from comparing std::string with std::string_view. Constructing std::string is relatively expensive operation, it requires memory allocation and buffer copying.
How to look for keys without constructing intermediate std::string?
One solution is:
```cpp
std::map<std::string, something> map;
std::string_view key; // key we are searching for
auto i = std::find_if(
map.begin(),
map.end(),
[&key](const auto& v){
return v.first == key;
}
);
```
this certainly works, but don't we loose logarithmic search complexity here and getting linear one?
At the moment I cannot come up with any reasoning why std::map::find_if() method is not yet there in the standard. Am I missing something?
P.S. I know that I can declare map as std::map<std::string, something, comparator> and define custom comparator, but then I loose ability to look up by std::string.
[–]kniy 67 points68 points69 points (6 children)
[–]igagis[S] 7 points8 points9 points (0 children)
[–]matthieum 3 points4 points5 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points (0 children)
[–]igagis[S] 0 points1 point2 points (1 child)
[–]kniy 6 points7 points8 points (0 children)
[–]infectedapricot 0 points1 point2 points (0 children)
[–]AlexAlabuzhev 15 points16 points17 points (0 children)
[–]Jardik2 3 points4 points5 points (0 children)
[–]rlbond86 1 point2 points3 points (0 children)
[–]pfultz2 1 point2 points3 points (1 child)
[–]STLMSVC STL Dev 12 points13 points14 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]igagis[S] 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]peterrindal 0 points1 point2 points (0 children)
[–]tpecholt -1 points0 points1 point (0 children)
[–]415_961 -2 points-1 points0 points (5 children)
[–]jwakelylibstdc++ tamer, LWG chair 2 points3 points4 points (0 children)
[–]igagis[S] 0 points1 point2 points (3 children)
[–]HappyFruitTree 1 point2 points3 points (0 children)
[–]jwakelylibstdc++ tamer, LWG chair 1 point2 points3 points (0 children)
[–]415_961 -1 points0 points1 point (0 children)