vector<string>初始化的几种方式

(1)第一种:类似于数组的方式:

std::vector<std::string> strArray(10);
strArray[0] = "hello";
strArray[1] = "world";
strArray[2] = "this";
strArray[3] = "find";
strArray[4] = "gank";
strArray[5] = "pink";
strArray[6 ]= "that";
strArray[7] = "when";
strArray[8] = "how";   
strArray[9] = "cpp";

(2)第二种:push_back的方式:

vector<string> strArray;
strArray.push_back("hello");
strArray.push_back("world");
strArray.push_back("this");
strArray.push_back("find");
strArray.push_back("gank");
strArray.push_back("pink");
strArray.push_back("that");
strArray.push_back("when");
strArray.push_back("how");   
strArray.push_back("cpp");

(3)第三种:构造函数的方式:
string str[]={“hello”,“world”,“this”,“find”,“gank”,“pink”,“that”,“when”,“how”,“cpp”};
vector strArray(str, str+10);

猜你喜欢

转载自blog.csdn.net/pj_wxyjxy/article/details/130485669