#include
#include
using namespace std;
int main()
{
int int_size;
string str;
cout << "請輸入一個字符串";
cin >> str;
int_size = sizeof(str);
cout << str<<"所占的字節大小為:" << int_size << endl;
system("pause");
return 0;
}
你這個寫的是錯的,sizeof返回類型的長度,應該用length
#include <iostream>
#include<string>
using namespace std;
int main()
{
int int_size;
string str = "aaaaaaaaaaaaaaaaaa";
int_size = str.length(); //sizeof(str);
cout << str<<"所占的字節大小為:" << int_size << endl;
return 0;
}