0

I have a map test and its initialize to below

test["auto works"] = 1; test["before word"] = 2; test["before list"] = 3; test["before pattern"] = 4; test["before me"] = 5; test["before hen"] = 6; test["has float"] = 7; 

And i have a string search which is initialized to "before me grep lot".

Now i want to find the search string in test map. Ideally i want look a better match for search string "before me grep lot" in test map.

The output should be 5.

please help me out.

2
  • 1
    Why is 5 the "better match"? Commented Jun 20, 2015 at 11:26
  • Do you want to return all the keys that begin with the search string? Commented Jun 20, 2015 at 11:40

2 Answers 2

1

Use test.lower_bound(search) (documentation).

Sign up to request clarification or add additional context in comments.

Comments

1

Try the following approach

#include <iostream> #include <string> #include <map> #include <algorithm> #include <iterator> int main() { std::map<std::string, int> test; test["auto works"] = 1; test["before word"] = 2; test["before list"] = 3; test["before pattern"] = 4; test["before me"] = 5; test["before hen"] = 6; test["has float"] = 7; std::string s( "before me grep lot" ); auto it = test.lower_bound( s ); size_t prev = 0, next = 0; if ( it != test.begin() ) { auto pos = std::mismatch( s.begin(), s.end(), std::prev( it )->first.begin(), std::prev( it )->first.end() ); prev = std::distance( s.begin(), pos.first ); } if ( it != test.end() ) { auto pos = std::mismatch( s.begin(), s.end(), it->first.begin(), it->first.end() ); prev = std::distance( s.begin(), pos.first ); } std::string target = prev < next ? it->first : std::prev( it )->first; std::cout << "The closest item is test[\"" << target << "\"] = " << test[target] << std::endl; } 

The program output is

The closest item is test["before me"] = 5 

If your compiler standard library does not support algorithm std::mismatch with four parameters then the if statements can look like

if ( it != test.begin() ) { std::cout << std::prev( it )->first << std::endl; std::string::size_type n = std::min( s.size(), std::prev( it )->first.size() ); auto pos = std::mismatch( s.begin(), std::next( s.begin(), n ), std::prev( it )->first.begin() ); prev = std::distance( s.begin(), pos.first ); } if ( it != test.end() ) { std::cout << it->first << std::endl; std::string::size_type n = std::min( s.size(), std::prev( it )->first.size() ); auto pos = std::mismatch( s.begin(), std::next( s.begin(), n ), it->first.begin() ); prev = std::distance( s.begin(), pos.first ); } 

3 Comments

Thanks a lot. but when i compile, i get below error:
sample.cpp(165): error C2064: term does not evaluate to a function taking 1 arguments sample.cpp(165): error C2227: left of '->first' must point to class/struct/union/generic type sample.cpp(165): error C2228: left of '.begin' must have class/struct/union sample.cpp(166): error C2064: term does not evaluate to a function taking 1 arguments 1>sample.cpp(166): error C2227: left of '->first' must point to class/struct/union/generic type sample.cpp(166): error C2228: left of '.end' must have class/struct/union std::mismatch(_InIt1,_InIt1,_InTy (&)[_InSize])' : expects 3 arguments - 4 provided
@S Sriniamul It seems that you have an old llibrary that does not support algorithm std::mismatch with four parameters. Wait a minute I will show how to use the old algorithm.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.