C++(23):contains检查字符串是否包含子字符串
经常需要检查字符串是否包含子字符串,C++23通过提供contains完成这项检查:
#include <iostream>
#include <string>int main()
{std::string str = "Hello, World!";bool contains_world = str.contains("World");std::cout << "Hello, World! has 'World'? " << (contains_world? "Yes" : "No") << std::endl;return 0;
}
编译:g++ -o m m.cpp -std=c++23
运行输出:Hello, World! has 'World'? Yes