#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("hello");
string str2("hello");
string str3("world");
if (str1 != str2)
{
cout << "str1 不等于 str2" << endl;
}
if (str1 == str2)
{
cout << "str1 等于 str2" << endl;
}
if (str1>str3)
{
cout << "str1 大于 str3" << endl;
}
else
{
cout << "str1不大于str3" << endl;
}
if (str1.compare(str3)>0)
{
cout << "str1 大于 str3" << endl;
}
if (str1.compare(str3)<0)
{
cout << "str1 小于 str3" << endl;
}
if (str1.compare(str3)==0)
{
cout << "str1 等于 str3" << endl;
}
string str4(str3,2,3);
cout << str4 << endl;
if (str1.compare(str4)>0)
{
cout << "str1 大于 str4" << endl;
}
else
{
cout << "str1 不等于于 str4" << endl;
}
if (str1.compare(2,2,str2)>0)
{
cout << "str1 大于 str2" << endl;
}
system("pause");
return 0;
}
=================================
str1 等于 str2
str1不大于str3
str1 小于 str3
rld
str1 不等于于 str4
str1 大于 str2
请按任意键继续. . .

本文详细介绍了在C++中如何使用标准库中的string类进行字符串的比较与操作,包括直接使用运算符如==、!=、>进行比较,以及使用compare方法进行更灵活的字符串对比。此外,还展示了如何通过指定起始位置和长度来创建子串。

8352

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



