having a map of strings how to compare it to given string
We have map of string pairs like name:location (unix like absolute location a la myfolder/
). We are given with some location a la myfolder/mysu开发者_StackOverflow中文版bfolder/myfile
. How to find which of maps location fit to given url most?
Example we have a map like:
service1:myfolder/
service2:myfolder/mysubfolder/
service3:myfolder/myothersubfolder/
service4:myfolder/mysubfolder/myfile
We are given value myfolder/mysubfolder/myfile/blablabla/
(string).
We want to find out to which item in our map it relates the most.
Search result shall be service4
as map item with most related content.
So how to find by given string value to which map element it relates the most?
Please provide some code because I am C++ nube and do not get how to inplement such thing?
So I simplified a problem a bit - now all relation I need is in how deep given path is which in string case can be aceived by just iteratin over all maps paths looking at thare langth , searching for appearence in given path and remembering most long map item path found in given path.
There are two options:
- If you need to run many queries:
- Build the inverse map or use bidirectional map.
- Find first larger element using upper_bound and
- If you need element with longest common prefix, check this and previous (last smaller) element and choose the one with longer common prefix.
- If you need element that is a prefix, scan back until you find an element that is a prefix.
- If you need just one query, simple linear search will be quicker (building the inverse map takes O(n log(n)), while one iteration takes just O(n)), plus it's easier to implement. Simply iterate over the map, for each value calculate the prefix length and remember the best match so far (I wanted to suggest using
std::max_element
, but it implements maximum by comparison operator while you need maximum by metrics).
If your map is defined like this:
typedef std::map<std::string,std::string> MyMap;
MyMap my_map;
...and the search term is defined like this:
std::string my_key_to_find = "service4";
...then you can get the value associated with this key like this:
std::string found_val;
MyMap::const_iterator it = my_map.find(my_key_to_find);
if( it != my_map.end() )
found_val = it->second;
else
std::cout << "Key not found!\n";
If I understand your question correctly, you want to search for keys by value (string), where the matching values are substrings of the provided search term. I don't think there is an easy solution for this as a general problem (i.e. arbitrary strings and all their substrings).
However, the strings used as values in your example have a specific structure (i.e. file system paths). You can exploit this structure to come up with a clean solution. First, make a bi-directional map. Then, implement the following lookup process:
- If path is empty, fail.
- Reverse lookup in the map based on the request path
- If found, return associated value.
- Pop the last component off the path.
- Loop.
If the list is short, you might just want to loop over the list of (key,value) pairs and select the key where the value is the most similar (i.e. longest substring in common).
精彩评论