vector容器是一个单端容器,形式如下,
这是一个在std名字空间中定义的模板类:
namespace std
{
template <class T, class Allocator = allocator<T> >
class vector;
}
如果使用vector必须包含头文件<vector>。
1 void test01(void) 2 { 3 vector<int> v1; 4 cout << "v1 size: " << v1.size() << endl; 5 showVector(v1); 6 v1.push_back(10); 7 v1.push_back(20); 8 cout << "v1 size: " << v1.size() << endl; 9 showVector(v1); 10 11 vector<int> v2(v1); 12 showVector(v2); 13 vector<int> v3(2); 14 showVector(v3); 15 vector<int> v4(10, 2); 16 showVector(v4); 17 vector<int> v5(v4.begin(), v4.end()); 18 showVector(v5); 19 20 }View Code
结果:
1 void test02() 2 { 3 vector<int> v1; 4 v1.push_back(10); 5 v1.push_back(20); 6 v1.push_back(30); 7 v1.push_back(40); 8 v1.push_back(50); 9 showVector(v1); 10 cout << "v1.size: " << v1.size() << endl; 11 cout << "v1.empty: " << v1.empty() << endl; 12 cout << "v1.max_size: " << v1.max_size() << endl; 13 cout << "v1.capacity: " << v1.capacity() << endl; 14 v1.reserve(10); 15 showVector(v1); 16 cout << "v1.size: " << v1.size() << endl; 17 cout << "v1.capacity: " << v1.capacity() << endl; 18 19 vector<int> v2; 20 vector<int> v3; 21 v2.push_back(1); 22 v2.push_back(2); 23 v2.push_back(3); 24 v3.push_back(1); 25 v3.push_back(2); 26 v3.push_back(3); 27 v3.push_back(4); 28 29 cout << "v2 > v3 ? ===" << (v2 > v3) << endl; 30 cout << "v2 > v3 ? ===" << (v2 < v3) << endl; 31 cout << "v2 > v3 ? ===" << (v2 >= v3) << endl; 32 cout << "v2 > v3 ? ===" << (v2 <= v3) << endl; 33 cout << "v2 > v3 ? ===" << (v2 == v3) << endl; 34 cout << "v2 > v3 ? ===" << (v2 != v3) << endl; 35 }View Code
结果:
1 void test03() 2 { 3 vector<int> v1; 4 vector<int> v2; 5 6 v1.push_back(10); 7 v1.push_back(20); 8 v1.push_back(30); 9 v2.push_back(1); 10 v2.push_back(2); 11 v2.push_back(3); 12 showVector(v1); 13 showVector(v2); 14 v1.assign(10, 44); 15 showVector(v1); 16 v2.assign(v1.begin(), v1.end()); 17 showVector(v2); 18 v1.push_back(123); 19 showVector(v1); 20 v1.swap(v2); 21 showVector(v1); 22 showVector(v2); 23 24 swap(v1, v2); 25 showVector(v1); 26 showVector(v2); 27 28 vector<int> v3; 29 showVector(v3); 30 v3 = v2; 31 showVector(v3); 32 }View Code
结果:
1 void test04(void) 2 { 3 vector<int> v1; 4 v1.push_back(10); 5 v1.push_back(20); 6 v1.push_back(30); 7 v1.push_back(40); 8 showVector(v1); 9 cout << v1.at(2) << endl; 10 cout << v1[2] << endl; 11 cout << v1.front() << endl; 12 cout << v1.back() << endl; 13 }View Code
结果:
1 void test05(void) 2 { 3 vector<int> v1; 4 v1.push_back(10); 5 v1.push_back(20); 6 v1.push_back(30); 7 v1.push_back(40); 8 v1.push_back(50); 9 v1.push_back(60); 10 11 for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) 12 { 13 cout << (*it) << " "; 14 } 15 cout << endl; 16 17 for (vector<int>::reverse_iterator it = v1.rbegin(); it != v1.rend(); it++) 18 { 19 cout << (*it) << " "; 20 } 21 cout << endl; 22 }View Code
结果:
1 void test06() 2 { 3 vector<int> v1; 4 v1.push_back(10); 5 v1.push_back(20); 6 v1.push_back(30); 7 v1.push_back(40); 8 showVector(v1); 9 v1.insert(v1.begin() + 1, 23); 10 showVector(v1); 11 12 v1.insert(v1.begin() + 2, 10, 33); 13 showVector(v1); 14 vector<int> v2; 15 v2.push_back(1); 16 v2.push_back(2); 17 v1.insert(v1.begin(), v2.begin(), v2.end()); 18 showVector(v1); 19 v1.pop_back(); 20 showVector(v1); 21 v1.erase(v1.begin()); 22 showVector(v1); 23 v1.erase(v1.begin() + 1, v1.end() - 1); 24 showVector(v1); 25 v1.resize(5); 26 showVector(v1); 27 v1.resize(10, 45); 28 showVector(v1); 29 v1.clear(); 30 showVector(v1); 31 }View Code
结果:
可以像使用动态数组一样使用vector。
举例:
1 void test08(void) 2 { 3 vector<char> v1; 4 v1.resize(40); 5 strcpy(&v1[0], "hello World"); 6 printf("%s\n", &v1[0]); 7 }View Code
输出hello World
成员函数中仅at()会抛出异常;