STLのvectorの初期化方式まとめ

1092 ワード

(1)配列と同様の方法:
std::vector<: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 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);