Case insensitive standard string comparison in C++ -


void main() {     std::string str1 = "abracadabra";     std::string str2 = "abracadabra";      if (!str1.compare(str2)) {         cout << "compares"     } } 

how can make work? bascially make above case insensitive. related question googled , here

http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx

there case insensitive method string::compare(str1, str2, bool). question how related way doing.

you can create predicate function , use in std::equals perform comparison:

bool icompare_pred(unsigned char a, unsigned char b) {     return std::tolower(a) == std::tolower(b); }  bool icompare(std::string const& a, std::string const& b) {     if (a.length()==b.length()) {         return std::equal(b.begin(), b.end(),                            a.begin(), icompare_pred);     }     else {         return false;     } } 

now can do:

if (icompare(str1, str)) {     std::cout << "compares" << std::endl; } 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -