I'm using a std::map
in my project, because I want to map several different strings to each other. For example I might create a map that looks similar to this:
std::map<std::string, std::string> map;
map["test"] = "Foo";
map["blah"] = "Drei";
map["fayh"] = "Najh";
// And so on...
I want to find these values using keys that are longer than the ones in the map, i.e partially match the keys. All keys in the map share the same prefix as the keys that they are compared against
This is what I am trying to achieve:
// Same map as earlier
std::cout << map.find('test123').second; // Should output 'Foo'
std::cout << map.find('test_2165').second; // Should output 'Foo' as well
std::cout << map.find('tes').second; // Key not found
std::cout << map.find('fayh_TK_Ka').second; // 'Najh'
I hope you understand what I am after. I want to efficiently retrieve values mapped to keys that correspond to compared keys which are larger than them, but share the same prefix (such as 'test').
I don't know if std::map
is the best choice in this case, if not, please do tell about other options.
NOTE: I have tried using a map with std::greater_equal
as key comparator combined with the lower_bound
method, but I end up with runtime errors, and I also question the efficiency of this approach.