string

一、string对象操作
string str
1、str.c_str() 返回const char*

2、两个字符串不能挨着相加

"abc" + "de" // 错误,
// 因为两个字符串直接相加,不知道得到的是什么类型,会导致编译错误;
"abc" + s + "de" //正确,
// 其中s 为字符串对象,当字符串和一个变量相加之后,返回值会转变成s 的类型,然后和后面的"de" 相加也变成了字符串对象和字符串相加,返回值也会值字符串对象

当然,直接初始化的时候赋值,是可以将字符串转变成字符串对象

3、for语句的使用

string str = "I Love China!";
for (auto c : str) // 这个auto 可以用char 类型替换
{
    cout << c << endl; //其中会输出str中的每个字符
}
for (auto& c : str) // 使用auto& 则可以修改其中的值
{
    c = toupper(c); //把小写变成大写字母
}

 

posted @ 2020-06-07 11:54  min_zhi  阅读(154)  评论(0编辑  收藏  举报