Vector容器(C++)

3.2 vector 容器

3.2.1 vector 基本概念

功能:

  • vector 数据结构和数组非常相似,也称为单端数组

vector与普通数组区别:

  • 不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

  • 并不是在原空间之后续接新的空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
  • vector 容器的迭代器是支持随机访问的迭代器
3.2.2 vector 构造函数

功能描述:

  • 创建vector容器

函数原型:

  • vector<T> v; //采用模板实现类实现,默认构造函数
  • vector(v.begin(),v.end()); //将v[begin(),end()]区间中的元素拷贝给本身
  • vector(n,elem); //构造函数将n个elem拷贝给本身
  • vector(const vector& vec); //拷贝构造函数

示例:

//vector构造函数
void test01() {
	//默认构造 无参构造
	vector<int>v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	//通过区间方式进行构造
	vector<int>v2(v1.begin(), v1.end());
	printVector(v2);

	//n个elem方式构造
	vector<int>v3(10, 100);
	printVector(v3);

	//拷贝构造参数
	vector<int>v4(v3);
	printVector(v4);
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
3.2.3 vector 赋值操作

功能描述:

  • 给vector 容器进行赋值

函数原型:

  • vector& operator=(const vector& vec);//重载等号操作符
  • assign(beg,end);//将[beg,end]区间中的数据拷贝赋值给本身
  • assign(n,elem);//将n个elem拷贝赋值给本身
#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;
}

//vector赋值操作
void test01() {
	vector<int>v1;
	for (int i = 0; i < 5; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	vector<int>v2 = v1;
	printVector(v2);

	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	vector<int>v4;
	v4.assign(5, 10);
	printVector(v4);
}


//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
3.2.4 vector 容量和大小

功能描述:

  • 对vector 容器的容量和大小操作

函数原型:

  • empty(); //判断容器是否为空

  • capacity(); //容器的容量

  • size(); //返回容器中元素的个数

  • resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置

    ​ //如果容器变短,则末尾超出容器长度的元素被删除

  • resize(int num,elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置

    ​ //如果容器变短,则末尾超出容器长度的元素被删除

示例:

#include<iostream>
#include<vector>
using namespace std;

void PrintVec(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}


//vector容量和大小
void test01() {
	vector<int>v1;
	for (int i = 0; i < 7; i++) {
		v1.push_back(i);
	}
	PrintVec(v1);
	
	if (v1.empty()) {
		cout << "v1 为空" << endl;
	}
	else {
		cout << "v1 不为空" << endl;
	}
	cout << "v1的容量为:" << v1.capacity() << endl;

	v1.resize(10);
	PrintVec(v1);

	v1.resize(5);
	PrintVec(v1);

	v1.resize(10, 10);
	PrintVec(v1);

	cout << "v1大小为:" << v1.size() << endl;
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}

总结:

  • 判断是否为空 — empty
  • 返回元素个数 — size
  • 返回容器容量 — capacity
  • 重新指定大小 — resize
3.2.5 vector 插入和删除

功能描述:

  • 对vector容器进行插入、删除操作

函数原型:

  • push_back(ele); //尾部插入元素ele
  • pop_back(); //删除最后一个元素
  • insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
  • insert(const_iterator pos, int count, ele); //迭代器指向位置pos插入count个元素ele
  • erase(const_iterator pos); //删除迭代器指向的元素
  • erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
  • clear(); //删除容器中所有元素

示例:

#include<iostream>
#include<vector>
using namespace std;

void PrintVec(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//vector插入和删除
void test01() {
	vector<int>v1;
	for (int i = 0; i < 5; i++) {
		v1.push_back(i);
	}
	PrintVec(v1);

	v1.pop_back();
	PrintVec(v1);

	v1.insert(v1.begin(), -1);
	PrintVec(v1);

	v1.insert(v1.end(), 3, 6);
	PrintVec(v1);

	v1.erase(v1.begin());
	PrintVec(v1);

	v1.clear();
	PrintVec(v1);

}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}

总结:

  • 尾插 — push_back
  • 尾删 — pop_back
  • 插入 — insert(位置迭代器)
  • 删除 — erase(位置迭代器)
  • 清空 — clear
3.2.6 vector 数据存取

功能描述:

  • 对vector 中的数据做存取操作

函数原型:

  • at(int idx);//返回索引[idx]所指的数据
  • operator[idx];//返回索引[idx]所指的数据
  • front();//返回容器中第一个数据元素
  • back();//返回容器中最后一个数据元素
#include<iostream>
#include<vector>
using namespace std;

//vector数据存取
void test01() {
	vector<int>v;
	v.push_back(0);
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);

	cout << "第四个元素是" << v.at(3) << endl;
	cout << "第三个元素是" << v[2] << endl;
	cout << "最前面的元素是" <<v.front() << endl;
	cout << "最后面的元素是" << v.back() << endl;
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
3.2.7 vector 互换容器

功能实现:

  • 实现两个容器内元素进行互换

函数原型:

  • swap(vec);//将vec与本身元素互换

示例:

#include<iostream>
#include<vector>
using namespace std;

void PrintVec(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//vector互换容器

//1、基本使用
void test01() {
	vector<int>v1(5, 5);
	vector<int>v2(4, 4);
	cout << "v1 = ";
	PrintVec(v1);
	cout << "v2 = ";
	PrintVec(v2);
	v1.swap(v2);
	cout << "swap v1 = ";
	PrintVec(v1);
	cout << "swap v2 = ";
	PrintVec(v2);
}

//2、实际用途
///巧用swap可以收缩空间
void test02() {
	vector<int>v;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
	}
	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	v.resize(3);
	cout << "重新指定大小后v的容量为:" << v.capacity() << endl;
	cout << "重新指定大小后v的大小为:" << v.size() << endl;

	//巧用swap收缩内存
	//vector<int>(v)//匿名对象
	// .swap(v)//容器交换
	vector<int>(v).swap(v);
	cout << "收缩内存后v的容量为:" << v.capacity() << endl;
	cout << "收缩内存后v的大小为:" << v.size() << endl;
}

//主函数
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}

总结:swap可以使两个容器互换,可以达到实用的收缩内存效果

3.2.8 vector 预留空间

功能描述:

  • 减少vector 在动态扩展容量时的扩展次数

函数原型:

  • reverse(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问

示例:

#include<iostream>
#include<vector>
using namespace std;

//vector预留空间
void test01() {
	vector<int>v1;
	
	int count1 = 0;//统计开辟次数
	int* p1 = NULL;
	for (int i = 0; i < 100000; i++) {
		v1.push_back(i);

		if (p1 != &v1[0]) {
			p1 = &v1[0];
			count1++;
		}
	}
	cout << "count = " << count1 << endl;

	//利用reserve预留空间
	vector<int>v2;
	v2.reserve(100000);
	int count2 = 0;//统计开辟次数
	int* p2 = NULL;
	for (int i = 0; i < 100000; i++) {
		v2.push_back(i);

		if (p1 != &v2[0]) {
			p1 = &v2[0];
			count2++;
		}
	}
	cout << "count = " << count2 << endl;
}


//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三月江东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值