您的位置:首页 > 其它

STL的vector<string>的三种简单初始化方式

2015-10-20 16:46 405 查看
(1)第一种,类似于数组的方式:

[cpp] view
plaincopyprint?





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的方式:

[cpp] view
plaincopyprint?





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)构造函数的方式:

[cpp] view
plaincopyprint?





string str[]={"hello","world","this","find","gank","pink","that","when","how","cpp"};  

vector<string> strArray(str, str+10);  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: