#include <iostream>
using namespace std;
int main()
{
string s("hello, world");
string head("hello");
string tail("ld");
bool startwith = s.compare(0, head.size(), head) == 0;
cout << boolalpha << startwith << endl;
bool endwith = s.compare(s.size() - tail.size(), tail.size(), tail) == 0;
cout << boolalpha << endwith << endl;
}
本文通过一个简单的C++程序示例介绍了如何使用标准库中的字符串类来判断一个字符串是否以特定的前缀或后缀开始或结束。该示例展示了如何使用compare方法结合子串截取来进行匹配。
1002

被折叠的 条评论
为什么被折叠?



