C++的字符串变量
1. string的定义和初始化
string str;
string str = "hello";
str = "world";
2. string的用法
- 字符串拷贝: =
- 字符串比较
- 大于: >
- 小于: <
- 等于: ==
- 不等于: !=
- 字符串的连接: + +=
- 获取字符串中某个字符: []
3. 示例
```cppinclude
include
// include
using namespace std;
int main(int argc, char const *argv[]) { cout << “————-c 风格字符串” << endl; char str1[36] = “hello”; char str2[36] = “world”; if(strcmp(str1, str2) < 0){ cout << “small str: “ << str1 << endl; }else{ cout << “small str: “ << str2 << endl; }
char str3[36] = {0};
strcpy(str3, str1);
cout << "str3 : " << str3 << endl;
strcat(str1, str2);
cout << "str1 : " << str1 << endl;
cout << "str1[4] : " << str1[4] << endl;
cout << "----------c++ 风格字符串" << endl;
string str11 = "hello";
string str12 = "world";
if(str11 < str12){
cout << "small str : " << str11 << endl;
}else{
cout << "small str : " << str12 << endl;
}
string str13;
str13 = str11;
cout << "str13 : " << str13 << endl;
//str11 += str12;
str11 = str11 + str12;
cout << "str11 : " << str11 << endl;
cout << "str11[4] : " << str11[4] << endl;
return 0;
} ```