I have
std::vector<std::string> vec;
std::string myString;
and I need to find out if myString
is in vec
using case insensitive comaprisons.
I know I can use
find(vec.begin(), vec.end(), myString) != vec.end())
to answer the question "is myString in vec?" but that will do case sensitive comparisons. I need case insensitive comparisons.
The position is not important, I just want to know if myString is in vec or not.
You need to use std::tolower and std::find_if:
std::vector<std::string> vec = {"ALF", "B"};
std::string toSearch = "Alf";
auto itr = std::find_if(vec.begin(), vec.end(),
[&](auto &s) {
if ( s.size() != toSearch.size() )
return false;
for (size_t i = 0; i < s.size(); ++i)
if (::tolower(s[i]) == ::tolower(toSearch[i]))
return true;
return false;
}
);
if ( itr != vec.end()) {
std::cout << *itr << std::endl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With