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:
- How to check, whether some element (such as
string
orpair
) matches a pattern. - Assuming you have such a check, how to apply it to each element in a container.
The solutions to each part:
- You can implement a function that takes a
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. - You can apply this function on every element of your container using find_if algorithm.
精彩评论