有一对如何查找它是否属于 map中某个对的一部分?
我们有一对字符串,例如这样的对 Accept-Language : RU ,
,我们通过映射进行搜索,例如 http 请求标头。我们需要知道映射中是否存在这样的对 - 一个布尔值。如何进行软搜索,这意味着我们不需要找到完全相同的对,而是像Accept-Language这样的对:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4 对我们来说也是一个有效的对,如果存在,我们可以认为我们已经发现我们的地图包含我们的对。如何在 C++ 中创建一个执行此类搜索的函数?
We have a pair of strings for example such pair Accept-Language : RU ,
and we search thru map, for example of http request headers. All we ned to know if there is such pair in map or not - a bool value. How to do to a soft search meaning we do not need to find exact same pair but pair like Accept-Language : ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
is also a valid pair for us and if such exists we can think we have found that our map contains our pair. How to make a function for performing such search in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果您使用
map
,则不能有多个具有相同键的条目。例如,您不能同时拥有Accept-Language : RU
和Accept-Language : ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
因为它们具有相同的密钥“Accept-Language”。也许在你的情况下你应该使用成对的向量,或者多重映射。接下来,您的问题由两部分组成:
string
或pair
)匹配一个模式。
这样的检查,如何应用它
容器中的每个元素。
每个部分的解决方案:
First of all, if you are using a
map
, you cannot have multiple entries with the same key. E.g. you can't have bothAccept-Language : RU
andAccept-Language : ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
because they have the same key `Accept-Language'. Perhaps in your case you should use a vector of pairs, or a multimap.Next, your question consists of 2 parts:
string
orpair
)matches a pattern.
such a check, how to apply it to
each element in a container.
The solutions to each part:
string
, or apair
(depends on the type of container and stored element that you choose), and checks whether it matches your criteria. You can find the functions such as string::find_first_of to be useful for that matter. The regex libraries can be even more helpful, though they are not part of the STL.