How to initialize an std::string with a length?

后端 未结 5 717
盖世英雄少女心
盖世英雄少女心 2020-12-30 02:29

If a string\'s length is determined at compile-time, how can I properly initialize it?

#include 
int length = 3;
string word[length]; //invalid         


        
5条回答
  •  春和景丽
    2020-12-30 02:49

    You can initialize your string like this:

    string word = "abc"
    

    or

    string word(length,' ');
    word[0] = 'a';
    word[1] = 'b';
    word[2] = 'c';
    

提交回复
热议问题