开发者

Having a pair<string, string> how to find if its part of some pair in map<string, string>?

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 开发者_JAVA百科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++?


First of all, if you are using a map, you cannot have multiple entries with the same key. E.g. you can't have both Accept-Language : RU and Accept-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:

  1. How to check, whether some element (such as string or pair) matches a pattern.
  2. Assuming you have such a check, how to apply it to each element in a container.

The solutions to each part:

  1. You can implement a function that takes a string, or a pair (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.
  2. You can apply this function on every element of your container using find_if algorithm.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜