#include <bits/stdc++.h>
using namespace std;
int main()
{
string n;
n="bcdef";
cout << n[0] << n[1] << n[2] << n[3] << n[4] << ' ' <<n.length()<< endl;
n="aa";
cout << n[0] << n[1] << n[2] << n[3] << n[4] << ' ' <<n.length()<< endl;
return 0;
}
结果:
bcdef 5
aa ef 2
我用string定义一个字符串"bcdef"并按字符数组形式输出,结果为bcdef 5
然后我将字符串重新赋值为“aa",再一次按字符数组形式输出,结果为aa ef 2
可以看到新的字符串已经赋值好,长度也是对的,但后面的e(对应n[3]) f(对应n[4])依旧放在缓存中。
想问一下有没有什么操作可以清除缓存中后面的数据,我试过clear()没有作用!
望大佬指点!