#include <iostream> #include <vector> using namespace std; void vec_test(vector<int> &v, int num, const char *ttl) { cout << "[" << ttl << "]" << endl; cout << "size() = " << v.size() << ", "; cout << "capacity() = " << v.capacity() << endl; int i; for (i = 0; i < num; i++) v[i] = i; try { for (i = 0; i < num; i++) v.at(i) = i; } catch (.../*out_of_range*/) { // gccで何か怒られたので誤魔化す cout << "out of range " << i << endl; } if (v.size()) { v.pop_back(); v.push_back(999); } cout << "v = "; for (i = 0; i < int(v.size()); i++) cout << v[i] << ", "; cout << endl; cout << endl; } int main(void) { vector<int> v0; vec_test(v0, 0, "1)v0定義直後"); vector<int> v(3); vec_test(v, 3, "2)v(3)定義直後"); v.clear(); v.resize(5); vec_test(v, 5, "3)clr, rsz 5, loop 5"); v.clear(); v.resize(4); v.reserve(4); vec_test(v, 4, "4)clr, rsz 4, rsrv 4, loop 4"); v.clear(); v.reserve(9); vec_test(v, v.size(), "5)clr, rsrv 9, loop v.size"); v.clear(); v.resize(2); v.reserve(6); vec_test(v, 6, "6)clr, rsz 2, rsrv 6, loop 6"); v.clear(); v.resize(7); v.reserve(4); vec_test(v, 7, "7)clr, rsz 7, rsrv 4, loop 7"); v.pop_back(); v.pop_back(); vec_test(v, 5, "8)pop,pop, loop 5"); v.clear(); vec_test(v, 0, "9)clr"); return 0; }
vc | gcc | bcc |
---|---|---|
[1)v0定義直後] size() = 0, capacity() = 0 v = [2)v(3)定義直後] size() = 3, capacity() = 3 v = 0, 1, 999, [3)clr, rsz 5, loop 5] size() = 5, capacity() = 5 v = 0, 1, 2, 3, 999, [4)clr, rsz 4, rsrv 4, loop 4] size() = 4, capacity() = 4 v = 0, 1, 2, 999, [5)clr, rsrv 9, loop v.size] size() = 0, capacity() = 9 v = [6)clr, rsz 2, rsrv 6, loop 6] size() = 2, capacity() = 6 out of range 2 v = 0, 999, [7)clr, rsz 7, rsrv 4, loop 7] size() = 7, capacity() = 7 v = 0, 1, 2, 3, 4, 5, 999, [8)pop,pop, loop 5] size() = 5, capacity() = 7 v = 0, 1, 2, 3, 999, [9)clr] size() = 0, capacity() = 0 v = |
[1)v0定義直後] size() = 0, capacity() = 0 v = [2)v(3)定義直後] size() = 3, capacity() = 3 v = 0, 1, 999, [3)clr, rsz 5, loop 5] size() = 5, capacity() = 5 v = 0, 1, 2, 3, 999, [4)clr, rsz 4, rsrv 4, loop 4] size() = 4, capacity() = 5 v = 0, 1, 2, 999, [5)clr, rsrv 9, loop v.size] size() = 0, capacity() = 9 v = [6)clr, rsz 2, rsrv 6, loop 6] size() = 2, capacity() = 9 out of range 2 v = 0, 999, [7)clr, rsz 7, rsrv 4, loop 7] size() = 7, capacity() = 9 v = 0, 1, 2, 3, 4, 5, 999, [8)pop,pop, loop 5] size() = 5, capacity() = 9 v = 0, 1, 2, 3, 999, [9)clr] size() = 0, capacity() = 9 v = |
[1)v0定義直後] size() = 0, capacity() = 0 v = [2)v(3)定義直後] size() = 3, capacity() = 3 v = 0, 1, 999, [3)clr, rsz 5, loop 5] size() = 5, capacity() = 5 v = 0, 1, 2, 3, 999, [4)clr, rsz 4, rsrv 4, loop 4] size() = 4, capacity() = 5 v = 0, 1, 2, 999, [5)clr, rsrv 9, loop v.size] size() = 0, capacity() = 9 v = [6)clr, rsz 2, rsrv 6, loop 6] size() = 2, capacity() = 9 out of range 2 v = 0, 999, [7)clr, rsz 7, rsrv 4, loop 7] size() = 7, capacity() = 9 v = 0, 1, 2, 3, 4, 5, 999, [8)pop,pop, loop 5] size() = 5, capacity() = 9 v = 0, 1, 2, 3, 999, [9)clr] size() = 0, capacity() = 9 v = |
#include <iostream> #include <vector> using namespace std; struct Clss { int val_; Clss() : val_(0) {printf("\tClss\n");} Clss(const Clss &b) : val_(b.val_) {printf("\tcpy(%d)\n",val_);} Clss(int n) : val_(n) {printf("\t:=%d\n",val_);} ~Clss() {printf("\t~Clss\n"); val_ = 0;} }; int main(void) { { printf("vector<Clss> {\n"); vector<Clss> v; printf("} size=%d capa=%d\n", v.size(), v.capacity()); //printf("resize 2 {\n"); //v.resize(2); //printf("} size=%d capa=%d\n", v.size(), v.capacity()); printf("reserve 5 {\n"); v.reserve(5); printf("} size=%d capa=%d\n", v.size(), v.capacity()); printf("resize 3 {\n"); v.resize(3); printf("} size=%d capa=%d\n", v.size(), v.capacity()); printf("[4]:=99 {\n"); v[4] = 99; printf("}\n"); printf("clear {\n"); v.clear(); printf("}\n"); printf("resize 2 {\n"); v.resize(2); printf("} size=%d capa=%d\n", v.size(), v.capacity()); printf("[4]=%d\n", v[4].val_); #if 0 printf("初期化のマネ {\n"); { Clss tmp; for (int i = 0; i < 2; i++) new((void*)&v[i]) Clss(tmp); } printf("}\n"); #endif } printf("main end\n"); return 0; }
vc | gcc | bcc |
---|---|---|
vector |
vector |
vector |