vector初始化的几种方式

三世轮回 提交于 2020-02-06 00:43:49
(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<string> strArray(str, str+10);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!