== // 关系操作符,从两个字符串的首字符根据字符的ASCII值开始比较
>
<
>=
<=
!=
+ //s1 + s2,把 s1 和 s2 连接成一个新字符串, 返回新生成的字符串
+= // s1 += s2, 把 s2追加到s1中
[] // str[n], 返回 str 中位置为 n 的字符,位置从 0 开始计数
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main(void)
{
string str1("Hello, ");
string str2 = "world!";
string str3 = str1 + str2;
string str4("Hello, world!");
cout << "str1: " << str1 << endl;
cout << "str5[1]: " << str5[1] << endl;
if(str1 > str2)
cout << "str2: " << str2 << endl;
cout << "str3: " << str3 << endl;
cout << "str4: " << str4 << endl;
if(str3 == str4)
{
cout << "str3 == str4" << endl;
}
string str5(str1);
str5 += str2;
cout << "str5: " << str5 << endl;
cout << "str5[1]: " << str5[1] << endl;
if(str1 > str2)
{
cout << str1 << "大于" << str2 << endl;
}
else if(str1 == str2)
{
cout << str1 << "等于" << str2 << endl;
}
else
{
cout << str1 << "小于" << str2 << endl;
}
return 0;
}
PC:~/WorkSpace/CPP/string/3.2.3$ ./str_operators.exe
str1: Hello,
str2: world!
str3: Hello, world!
str4: Hello, world!
str3 == str4
str5: Hello, world!
str5[1]: e
Hello, 小于world!