c++ sizeof( string )

后端 未结 8 1545
陌清茗
陌清茗 2020-12-09 13:59
#include 
#include 

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


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 14:54

    It isn't clear from your example what 'string' is. If you have:

    #include 
    using namespace std;
    

    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();
    

提交回复
热议问题