Created
May 22, 2015 04:44
-
-
Save MasazI/55d3744da52b08289b83 to your computer and use it in GitHub Desktop.
vector_resize.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // reserver_resize.cpp | |
| // CplusplusPractice | |
| // | |
| // Created by masai on 2015/05/22. | |
| // Copyright (c) 2015年 masai. All rights reserved. | |
| // | |
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| int main(int argc, char* argv[]){ | |
| // デフォルトコンストラクタ | |
| vector<int> vec_def; | |
| cout << "vec_def size: " << vec_def.size() << ", capacity: " << vec_def.capacity() << endl; | |
| // コンストラクタでサイズ指定 | |
| vector<int> vec_constructor(256); | |
| cout << "vec_constructor size: " << vec_constructor.size() << ", capacity: " << vec_constructor.capacity() << endl; | |
| // デフォルトコンストラクタで作成したvectorをreserveで変更 | |
| vec_def.reserve(128); | |
| cout << "vec_def reserve size: " << vec_def.size() << ", capacity: " << vec_def.capacity() << endl; | |
| // リサイズで変更 | |
| vec_def.resize(64); | |
| cout << "vec_def resize size: " << vec_def.size() << ", capacity: " << vec_def.capacity() << endl; | |
| // リサイズすると、size()も変更後と同じになる。出力すると、すべて0に初期化されていた。 | |
| for(auto& a : vec_def){ | |
| cout << a << " " ; | |
| } | |
| cout << endl; | |
| // リサイズでは、初期化する値を決定することができる | |
| vec_def.resize(128, 10); | |
| cout << "vec_def resize size: " << vec_def.size() << ", capacity: " << vec_def.capacity() << endl; | |
| for(auto& a : vec_def){ | |
| cout << a << " " ; | |
| } | |
| cout << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment