If I have a vector of strings how do I do a binary search for a certain string using a case-insensitive comparison? I can’t think of any easy way to do this.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Provide a comparison function to std::sort, sort your container in lower case (use boost string algos to help),
Then do a binary string on the sorted vector, again provide a case insensitive comparison operation to do this.
Using lambda expression will really help
If you use find it doesn’t have to be sorted first, however it is slow if you are going to doing frequent search and the set is quite large.
EDIT: here is the example
The same comparison function would also work with std::find if you are not going to sort the list.
TESTED
http://en.cppreference.com/w/cpp/algorithm/find
http://en.cppreference.com/w/cpp/algorithm/binary_search
http://en.cppreference.com/w/cpp/algorithm/sort