c++

c++ string匹配能否忽略大小写

小樊
81
2024-09-25 08:47:12
栏目: 编程语言

是的,C++中的string匹配可以忽略大小写。可以使用C++标准库中的algorithm函数tolower()将字符串转换为小写,然后再进行匹配。例如:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1 = "Hello World";
    string str2 = "hello world";
    transform(str1.begin(), str1.end(), str1.begin(), ::tolower); // 将str1转换为小写
    transform(str2.begin(), str2.end(), str2.begin(), ::tolower); // 将str2转换为小写
    if (str1 == str2) {
        cout << "Strings match, ignoring case." << endl;
    } else {
        cout << "Strings do not match." << endl;
    }
    return 0;
}

输出结果为:

Strings match, ignoring case.

0
看了该问题的人还看了