Tuesday 15 March 2011

c++ - Finding in a std map using regex -


I want to know how can I look for an item in a mat using a raggeon function. In my case, I have map such expressions as N *, ESE *, N-Au and so on and I have a string of possible values ​​like en, En -US, N-GB, SSL and so on.

I want to use that string to search for that item in that tag Before searching for key without wildcard, see the wildcard as the second priority.

Please help me with this problem or if it is incomplete or if someone has a different approach, please tell me another way for this, I encourage and use C ++ with STL < / P>

If the map is small or the search is rarely executed, then simply repeat through the map And make each match key with regular expressions.

Otherwise: if regular expression is used only for prefix detection of some kind, you can use the member function lower_bound In order to find all the entries efficiently with the given prefix, for example, the following function first appears for an entry that exactly matches. If no such entry is present, then the function returns the range of all entries with the mailing prefix.

  items = std :: map & lt; Std :: string, item & gt ;; Auto Lookup (Constant Items and Items, Constant Stroke :: String and Key) - & gt; Std :: Pair and LT; Item :: const_iterator, item :: const_iterator & gt; {Auto p = items.lower_bound (key); Auto q = items.end (); If (p! = Q & p-> first == key) {return std :: make_pair (p, std :: next (p)); } Else {auto r = p; While (R! = Q & amp; R-> FirstCopire (0, key.Size (), key) == 0) {++ r; } Return std :: make_pair (p, r); Otherwise: If you have to face  regular expression  or  wildcard , then you can add two ways. The first search for an entry that exactly matches the member function  Find . If no such entry is present, then remove the prefix continuously with regular expression. The prefix may be empty. Use the member function  lower_bound  to find the first entry with that prefix. Repeat through all the entries with that prefix and test if the regular expression matches.   

No comments:

Post a Comment