#include<vector>
#include<iostream>
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);
}
//赋值操作
void test02() {
//初始化两个容器
int arr[] = {
10, 20, 30, 40 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
int arr_[] = {
1, 2, 3, 4 };
vector<int> v2(arr_, arr_ + sizeof(arr_) / sizeof(int));
PrintVector(v1);
PrintVector(v2);
cout << "------交换后------" << endl;
v2.swap(v1); //交换
PrintVector(v1);
PrintVector(v2);
}
//大小操作
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() == true) {
cout << "容器为空!" << endl;
}
else {
cout << "容器不为空!" << endl;
}
cout << "原数据:";
PrintVector(v1);
cout << "缩小为2后:";
v1.resize(2);
PrintVector(v1);
cout << "再默认增大为5后:";
v1.resize(5); //默认增加的元素为0
PrintVector(v1);
cout << "再带参增大为8后:";
v1.resize(8, 4); //此处设定增加的元素为4
PrintVector(v1);
vector<int> v2;
for (int i = 0; i < 1000; i++) {
v2.push_back(i);
}
cout << "size:" << v2.size() << endl; //元素的个数
cout << "capacity:" << v2.capacity() << endl; //容器的容量
}
//存取操作
void test04() {
int arr[] = {
10, 20, 30, 40 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
cout << "[]重载 " << v1[0] << endl;
cout << "at方法 " << v1.at(3) << endl;
}
//插入和删除操作
void test05() {
vector<int> v;
v.push_back(10);
v.push_back(20);
//头插法
v.insert(v.begin(), 30); //在begin的位置插入30
v.insert(v.end(), 40); //在end的位置插入40
v.insert(v.begin() + 2, 100); //在迭代器为begin+2的位置插入100
//vector支持随机访问
//支持数组下标访问的,一般都支持随机访问
//迭代器可以直接+2 +3 -5等操作的,支持随机访问
PrintVector(v);
//删除
v.erase(v.begin()); //删除迭代器begin指向的元素
PrintVector(v);
v.erase(v.begin() + 1, v.end());//删除begin+1位置到end位置的所有元素
PrintVector(v);
v.clear(); //删除所有元素
cout << "clear后size:" << v.size();
}
//用swap缩减空间
void test06() {
vector<int> v;
for (int i = 0; i < 1000; i++)
v.push_back(i);
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
v.resize(22); //将元素缩减为100个
cout << "-- resize元素个数之后 --" << endl;
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
//收缩空间
vector<int>(v).swap(v);
cout << "-- 收缩空间之后 --" << endl;
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
}
//reserve和resize的区别
//用reserve预留空间提高程序效率
void test07() {
vector<int> v;
int num = 0;
int* address = NULL;
v.reserve(10000); //直接预留空间
for (int i = 0; i < 10000; i++) {
v.push_back(i);
//判断,如果首地址与之前的不同,则说明申请了新的空间
//当容量不足时,会申请新的内存,再将原来的数据进行拷贝
if (address != &(v[0])) {
address = &(v[0]);
num++;
}
}
cout << "重新申请的内存次数 num = " << num << endl;
}
//简单测试
int main() {
//test01();
//test02();
//test03();
//test04();
//test05();
//test06();
test07();
cout << endl << endl;
return 0;
}
黑马程序员C++提高3——vector容器
猜你喜欢
转载自blog.csdn.net/qq_43685399/article/details/108610485
今日推荐
周排行