vector cannot be initialized by {}

前端 未结 1 1284
夕颜
夕颜 2021-01-14 14:51

Hi I am a newbie in C++ and I just cannot initialize a vector using {}, even if the code is copied with a book. For example, when I do these

vector 

        
相关标签:
1条回答
  • 2021-01-14 15:46

    uniform initialization is introduced since C++11, You should use a recent compiler that supports this new feature.

    If your compiler does not support this feature, you may try the following:

    string arrOfString[3] =  {"a", "an", "the"};
    vector<string> articles(arrOfString, arrOfString +3);
    

    EDIT:

    with MSVC11, you can do (by courtesy of @chris):

    string arrOfString[3] =  {"a", "an", "the"};
    vector<string> articles(std::begin(arrOfString), std::end(arrOfString));
    
    0 讨论(0)
提交回复
热议问题