vector容器
一、简介:
vector容器是一个单端容器,形式如下,
这是一个在std名字空间中定义的模板类:
namespace std
{
template <class T, class Allocator = allocator<T> >
class vector;
}
如果使用vector必须包含头文件<vector>。
二、vector类的操作:
1、创建、复制和销毁

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 }
结果:
2、无修改操作:

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 }
结果:
3、赋值:

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 }
结果:
4、元素访问:

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 }
结果:
5、迭代器操作:

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 }
结果:
6、插入和删除元素:

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 }
结果:
7、把vector当做普通数组使用:
可以像使用动态数组一样使用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 }
输出hello World
8、异常处理:
成员函数中仅at()会抛出异常;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理