1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #include<iostream> using namespace std; #include<vector> #include<algorithm>
void test_vector1() { vector<int>v1; vector<int>v2(10,8); vector<int>v3(++v2.begin(),--v2.end()); vector<int>v4(v3); string s("hello world"); vector<char>v5(s.begin(),s.end());
}
void test_vector2() { vector<int> v; v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); int i=0; for(i=0;i<v.size();i++) { v[i]+=1; cout<<v[i]; } cout<<endl; vector<int>::iterator it=v.begin(); while(it!=v.end()) { *it-=1; cout<<*it; ++it; }
}
void test_vector3() {
vector<int> v; v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); v.pop_back(); v.assign(10,5); cout<<v.max_size()<<endl; v.reserve(50); v.resize(100); }
void test_vector4() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); vector<int>::iterator ret= find(v.begin(),v.end(),3); if(ret!=v.end()) { cout<<"1"<<endl; v.insert(ret,30); } int i=0; for(i=0;i<v.size();i++) { cout<<v[i];
} vector<int>::iterator ret1= find(v.begin(),v.end(),3); if(ret1!=v.end()) { v.erase(ret1); } v.clear(); cout<<v.capacity()<<endl; v.front(); v.back(); } void test_vector5() { vector<vector<int> > vv; vv.resize(5); cout<<vv.capacity()<<endl; cout<<vv.size()<<endl; int i=0;
for(i=0;i<5;i++) { vv[i].resize(i+1); vv[i].front()=vv[i].back()=1; } for(int i=0;i<vv.size(),i++) { for(int j=0;j<vv[i].size();j++)
{ if(vv[i][j]==0) { vv[i][j]=vv[i-1][j]+vv[i-1][j-1]; } } }
}
int main() { test_vector1(); test_vector2(); test_vector3(); test_vector4(); test_vector5();
return 0; }
|