c ++ sizeof(...) [英] c++ sizeof( ... )

查看:139
本文介绍了c ++ sizeof(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[])
        {
            cout << "size of String " << sizeof(string );

            system("PAUSE");
            return EXIT_SUCCESS;
        }

输出:

String size = 4

size of String = 4

这意味着,因为sizeof(char)= 1字节0到255),字符串只能包含4个字符?

Does that mean that, since sizeof(char) = 1 Byte (0 to 255), string can only hold 4 characters?

推荐答案

。如果您有:

#include <string>
using namespace std;

然后 string > std :: string 和 sizeof(std :: string)给出类实例及其数据成员的大小,的字符串。要得到它,请使用:

then string is std::string, and sizeof(std::string) gives you the size of the class instance and its data members, not the length of the string. To get that, use:

string s;
cout << s.size();

这篇关于c ++ sizeof(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆