1.[ ]运算符和at函数
返回的是string的当前字符串的合法的索引位置的引用,所谓的合法是指小于size的索引
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello";
cout<<"str:"<<str<<endl;
cout<<"size:"<<str.size()<<endl;
cout<<"capacity:"<<str.capacity()<<endl;
cout<<endl;
str[0] = 'e';
str.at(4) = 'l';
cout<<"str:"<<str<<endl;
cout<<"size:"<<str.size()<<endl;
cout<<"capacity:"<<str.capacity()<<endl;
return 0;
}
运行程序输出:
str:hello
size:5
capacity:15str:eelll
size:5
capacity:15可以看到字符串的内容发生了变化,但是长度和容量都没有变