初始化:
1. 默认构造:
vector<int> vint;
2. 用包含10个元素的数组初始化:
vector<int> vint(ia, ia+10);
算法:
1. vint.push_back(i);
2. vint.size();
3. vint[i];
4. vint.erase(pos1, pos2);
代码:
1 #include <vector> 2 #include <iostream> 3 #include <iterator> 4 using namespace std; 5 6 int ia[] = {1,2,3,4,5,6,7,8,9,10, 11,12}; 7 8 int main() { 9 //initialize 10 vector<int> vint1; 11 vector<int> vint2(ia, ia+10); 12 13 //push_back() 14 for(int i = 0; i <=6; ++i) 15 vint1.push_back(i); 16 17 //no push_front() 18 //vint1.push_front(222); 19 20 //size() 21 cout << "The size of vint1 is: " << vint1.size() << endl; 22 23 //[] operator 24 for(int i = 0; i < vint2.size(); i++) 25 cout << vint2[i] << endl; 26 27 vint2.erase(vint2.begin()+3, vint2.end()); 28 29 copy(vint2.begin(), vint2.end(), ostream_iterator<int>(cout, " ")); 30 31 return 0; 32 }
输出:
$ ./a.exe The size of vint1 is: 7 1 2 3 4 5 6 7 8 9 10 1 2 3