拥有字符串映射如何将其与给定字符串进行比较
我们有字符串对的映射,如 name:location (类似于 UNIX 的绝对位置,如 myfolder/
)。我们得到了一些位置,例如myfolder/mysubfolder/myfile
。如何找到哪个地图位置最适合给定的网址?
例如,我们有一个像这样的地图:
service1:myfolder/
service2:myfolder/mysubfolder/
service3:myfolder/myothersubfolder/
service4:myfolder/mysubfolder/myfile
我们被赋予值myfolder/mysubfolder/myfile/blablabla/
(字符串)。 我们想要找出它与地图中的哪个项目最相关。 搜索结果应为 service4
作为具有最相关内容的地图项。
那么如何根据给定的字符串值找到与哪个映射元素最相关的呢?
请提供一些代码,因为我是 C++ nube,不知道如何实现这样的事情?
所以我稍微简化了一个问题 - 现在我需要的所有关系是给定路径的深度,在字符串情况下,只需迭代所有地图路径,查看长度,搜索给定路径中的出现并记住最长的地图项即可获得在给定路径中找到的路径。
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/mysubfolder/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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两个选项:
There are two options:
std::max_element
, but it implements maximum by comparison operator while you need maximum by metrics).如果您的地图定义如下:
...并且搜索词定义如下:
...那么您可以获得与此键关联的值,如下所示:
If your map is defined like this:
...and the search term is defined like this:
...then you can get the value associated with this key like this:
如果我正确理解您的问题,您想按值(字符串)搜索键,其中匹配值是提供的搜索词的子字符串。我认为对于这个普遍问题(即任意字符串及其所有子字符串)没有一个简单的解决方案。
但是,示例中用作值的字符串具有特定的结构(即文件系统路径)。您可以利用这个结构来提出一个干净的解决方案。首先,制作一个双向地图。然后,执行以下查找过程:
如果列表很短,您可能只想循环(键,值)对列表并选择值最相似的键(即最长的公共子字符串)。
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 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).