vector容器 动态数组 可变数组
vector容器 单口容器
vector实现动态增长:
当插入新元素时,如果空间不足,那么vector会重新申请更大内存空间(默认二倍),将原空间数据拷贝到新空间,释放旧空间的数据,再把新元素插入新申请空间。
当我们知道我们存储的元素大概有多少时,使用reserve方法,减少vector重新申请内存-拷贝数据-释放旧空间的次数
#include<iostream>
#include<vector>
using namespace std;
void test01() {vector<int> v;int* p=NULL;int count = 0; //统计vector容量增长次数for (int i = 0; i < 100000; i++){v.push_back(i);if (p != &v[0]) {p = &v[0]; //内存首地址发生变化,申请新内存count++;}}cout << count << endl;
}
void test02() {vector<int> v;v.reserve(100000);int* p=NULL;int count = 0; //统计vector容量增长次数for (int i = 0; i < 100000; i++){v.push_back(i);if (p != &v[0]) {p = &v[0];count++;}}cout << count << endl;
}
int main() {test01();test02();
}
输出:
#include<iostream>
#include<vector>
using namespace std;void PrintVector(vector<int>& v) {for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}//初始化
void test01() {vector<int> v1; //默认构造int arr[] = { 10,20,30,40 };vector<int> v2(arr, arr + sizeof(arr) / sizeof(int));vector<int> v3(v2.begin(), v2.end());vector<int> v4(v3);PrintVector(v2);PrintVector(v3);PrintVector(v4);
}
/*
输出:
10 20 30 40
10 20 30 40
10 20 30 40
*///赋值操作
void test02()
{int arr[] = { 10,20,30,40 };vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));//成员方法vector<int> v2;v2.assign(v1.begin(), v1.end());//重载=号vector<int> v3;v3 = v2;cout << "v1" << endl;PrintVector(v1);cout << endl<<"-------" << endl;int arr1[] = { 100,200,300,400 };vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));v4.swap(v1); //将指针指向进行交换PrintVector(v1);PrintVector(v2);PrintVector(v3);PrintVector(v4);
}
/*
输出:
v1
10 20 30 40-------
100 200 300 400
10 20 30 40
10 20 30 40
10 20 30 40
*///大小操作
void test03() {int arr[] = { 10,20,30,40 };vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));cout << "size:" << v1.size() << endl;if (!v1.empty()) {cout << "v1 is not empty" << endl;}PrintVector(v1);v1.resize(2);PrintVector(v1);v1.resize(6);PrintVector(v1);v1.resize(8, 1);PrintVector(v1);for (int i = 0; i < 10000; i++){v1.push_back(i);}cout << "size:" << v1.size() << endl;cout << "capacity:"<<v1.capacity() << endl; //容量
}
/*
输出:
size:4
v1 is not empty
10 20 30 40
10 20
10 20 0 0 0 0
10 20 0 0 0 0 1 1
size:10008
capacity:12138
*///存储数据
void test04() {int arr[] = { 10,20,30,40 };vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));for (int i = 0; i < v1.size(); i++){cout << v1[i] << endl; //不抛异常}for (int i = 0; i < v1.size(); i++){cout << v1.at(i) << endl; //抛异常}//front第一个元素 back最后一个元素cout << "第一个元素front:" << v1.front() << endl;cout << "最后一个元素back:" << v1.back() << endl;
}
/*
输出:
10
20
30
40
10
20
30
40
第一个元素front:10
最后一个元素back:40
*///插入和删除
void test05() {vector<int> v;v.push_back(10);v.push_back(20);v.insert(v.begin(), 30);v.insert(v.end(), 50);v.insert(v.begin() + 2, 1000);//vector容器支持随机访问//支持数组下标访问,支持随机访问//迭代器可以直接+2 +3 -2等操作PrintVector(v);v.erase(v.begin());PrintVector(v);v.erase(v.begin() + 1, v.end());PrintVector(v);v.clear();cout << "size:" << v.size() << endl;}
/*
输出:
30 10 1000 20 50
10 1000 20 50
10
size:0
*/
//巧用swap缩减空间
void test06() {//vector 添加元素 自动增长 删除元素 不会自动减少vector<int> v;for (int i = 0; i < 10000; i++) {v.push_back(i);}cout << "size:" << v.size() << endl;cout << "capacity:" << v.capacity() << endl; //容量v.resize(10);cout << "=----------------" << endl;cout << "size:" << v.size() << endl;cout << "capacity:" << v.capacity() << endl; //容量//收缩空间// 1 vector<int>为匿名对象,// 2 vector<int>(v)用v去初始化匿名对象,会根据他自己大小去初始化自己// 3 vector<int>(v).swap(v) v与匿名对象交换内存指向,将匿名对象销毁vector<int>(v).swap(v);cout << "=----------------" << endl;cout << "size:" << v.size() << endl;cout << "capacity:" << v.capacity() << endl; //容量}
/*
输出:
size:10000
capacity:12138
=----------------
size:10
capacity:12138
=----------------
size:10
capacity:10
*/
int main()
{cout << "-------------test01--------------" << endl;test01();cout << "-------------test02--------------" << endl;test02();cout << "-------------test03--------------" << endl;test03();cout << "-------------test04--------------" << endl;test04();cout << "-------------test05--------------" << endl;test05();cout << "-------------test06--------------" << endl;test06();
}