这道题我编写的程序是:
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s1("hello,world");
cout<<sizeof(s1)<<endl;
}
结果算出来是16,怎么回事?请高手解释一下原因,好吗?并且写出正确的程序算法!
可是改为s1.length()后,结果算出的是1!这是怎么回事啊?
追答应该是11吧。这个我试了。
#include
#include
using namespace std;
void main()
{
string s1("hello,world");
cout<<s1.length()<<endl;
}
那也不对啊,争取的答案应该是13啊!怎么回事啊?
追答hello,world不是正好是11个字节码?你为什么要得到是13呢?释放时时13,是因为有结尾符的原因。