听着夜曲逛威尼斯 2015-09-21 10:27 采纳率: 100%
浏览 1890
已采纳

(C++)编了一个获取字符串字节数的程序,你们看看有什么要改进的?

#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;

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2015-09-21 14:05
    关注

    你这个写的是错的,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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?