Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial match for the key of a std::map

I have an std::map and I want to search for a key using a substring. For example, I have the following code:

#include <iostream> #include <map> #include <string> using namespace std;  typedef std::map<std::string, std::string> TStrStrMap; typedef std::pair<std::string, std::string> TStrStrPair;  int main(int argc, char *argv[]) {     TStrStrMap tMap;      tMap.insert(TStrStrPair("John", "AA"));     tMap.insert(TStrStrPair("Mary", "BBB"));     tMap.insert(TStrStrPair("Mother", "A"));     tMap.insert(TStrStrPair("Marlon", "C"));      return 0; } 

Now, I want to search for the position that holds the substring "Marl" and not "Marlon", if "Marla" is stored in the map. I want to find something that starts with "Marl". I need to find at most one position. Is this possible? If so, how?

I don't want to use any Boost libraries!

like image 451
cateof Avatar asked Feb 19 '12 13:02

cateof


People also ask

How do I find the key of a map in C++?

To check for the existence of a particular key in the map, the standard solution is to use the public member function find() of the ordered or the unordered map container, which returns an iterator to the key-value pair if the specified key is found, or iterator to the end of the container if the specified key is not ...

Can a map have 2 Keys C++?

Implement a MultiKeyMap in C++A MultiKeyMap is a map that offers support for multiple keys. It is exactly the same as a normal map, except that it needs a container to store multiple keys. A simple solution to implement a MultiKeyMap in C++ is using std::pair for the key.

What does std::map return if key not found?

std::map operator[] inserts the default constructed value type in to the map if the key provided for the lookup doesn't exist. So you will get an empty string as the result of the lookup.

What is the complexity of std::map :: insert () method?

Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.


1 Answers

You can't efficiently search for substring, but you can for prefix:

#include <iostream> #include <map> #include <string> #include <algorithm> using namespace std;  typedef map<string, string> TStrStrMap; typedef pair<string, string> TStrStrPair;  TStrStrMap::const_iterator FindPrefix(const TStrStrMap& map, const string& search_for) {     TStrStrMap::const_iterator i = map.lower_bound(search_for);     if (i != map.end()) {         const string& key = i->first;         if (key.compare(0, search_for.size(), search_for) == 0) // Really a prefix?             return i;     }     return map.end(); }  void Test(const TStrStrMap& map, const string& search_for) {     cout << search_for;     auto i = FindPrefix(map, search_for);     if (i != map.end())         cout << '\t' << i->first << ", " << i->second;     cout << endl; }  int main(int argc, char *argv[]) {     TStrStrMap tMap;      tMap.insert(TStrStrPair("John", "AA"));     tMap.insert(TStrStrPair("Mary", "BBB"));     tMap.insert(TStrStrPair("Mother", "A"));     tMap.insert(TStrStrPair("Marlon", "C"));      Test(tMap, "Marl");     Test(tMap, "Mo");     Test(tMap, "ther");     Test(tMap, "Mad");     Test(tMap, "Mom");     Test(tMap, "Perr");     Test(tMap, "Jo");      return 0; } 

This prints:

Marl    Marlon, C Mo      Mother, A ther Mad Mom Perr Jo      John, AA 
like image 98
Branko Dimitrijevic Avatar answered Sep 28 '22 09:09

Branko Dimitrijevic