I want the std::map
to use the comparator only while searching, e.g. the rest operations including the operation of insertion one must use the default one. Is it possible?
Asked
Active
Viewed 48 times
0

Ted Lyngmo
- 93,841
- 5
- 60
- 108

ghostinecatnewyear
- 293
- 3
- 11
-
1Sounds like you might be interested in a [multi-indexed container](https://www.boost.org/doc/libs/1_62_0/libs/multi_index/doc/index.html) – NathanOliver Mar 17 '20 at 16:26
-
Ted Lyngmo Sorry, I did mistake writing this question. I have just corrected it. – ghostinecatnewyear Mar 17 '20 at 16:31
-
Can you describe the use-case? It'll be very inefficient with a standard `map`, but there are other containers as @NathanOliver pointed out. – Ted Lyngmo Mar 17 '20 at 16:32
-
I expect insert to search for where to insert a new item. They aren't really separate operations. But NO's link looks like exactly what you want. – Kenny Ostrom Mar 17 '20 at 16:35
-
Do you want a [transparent comparator](https://stackoverflow.com/questions/20317413/what-are-transparent-comparators)? – HolyBlackCat Mar 17 '20 at 16:36
-
Ted Lyngmo I have a map with keys that are regular expressions (represented by strings). So when I want to find some value by the key, the map must check if the key matches to one of the map's regular expression. But the insertion must use the default string comparison logic. – ghostinecatnewyear Mar 17 '20 at 16:43
-
1@ghostinecatnewyear The only way to insert according to one logic and retrieve according to another is to do a linear search of the data structure. – btilly Mar 17 '20 at 16:57
-
@ghostinecatnewyear I assume that you use a `map` because you want the `regex`es sorted in lexicographical order to get a consistent result using this top-down filter on different platforms (since an `unordered_map` may sort them differently on different platforms)? Is there overlapping? Can one search term match multiple `regex`es in your `map`? What is the _value_ that the `regex` is mapped to? – Ted Lyngmo Mar 17 '20 at 17:28
1 Answers
0
I want the std::map to use the comparator only while searching ... Is it possible?
Well, you can do linear search on the map using whatever comparator you want. But that won't be as fast as using the search tree structure that the map has, which is built using the comparator of the map.
I have a map with keys that are regular expressions (represented by strings). So when I want to find some value by the key, the map must check if the key matches to one of the map's regular expression.
It seems that linear search is what you need indeed.

eerorika
- 232,697
- 12
- 197
- 326