C++ vector容器

vector容器基本概念

功能:
vector容器数据结构与数组非常相似,也称为单端数组
与普通数组的区别:
vector可以动态扩展
动态扩展:
不是在原空间之后续接新空间,而是找到更大的空间,然后将原空间的数据拷贝到新空间,再将原空间释放。

vector构造函数

函数原型:
vector v; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end()); //将v[begin(), v.end()](前闭后开)区间中的元素拷贝给本身
vector(n,elem); //构造函数将n个elem拷贝给本身
vector(const vector &vec); //拷贝构造函数

示例:

#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 << "  ";
	}
}

//vector容器构造
void _01Test01()
{
	//默认构造 
	vector<int> v1;
	PrintVector(v1);
	cout << "v1 :";
	for (int i = 0; i < 9; i++)
	{
		v1.push_back(i);
	}
	PrintVector(v1);
	cout << endl;

	//通过区间构造
	vector<int> v2(v1.begin(),v1.end());
	cout << "v2 :";
	PrintVector(v2);
	cout << endl;

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

	//拷贝构造
	vector<int>v4(v3);
	PrintVector(v4);
	cout << endl;

}

void main()
{
	_01Test01();
}

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 << "  ";
	}
}

//vector容器构造
void _01Test01()
{
	//默认构造 
	vector<int> v1;
	PrintVector(v1);
	cout << "v1 :";
	for (int i = 0; i < 9; i++)
	{
		v1.push_back(i);
	}
	PrintVector(v1);
	cout << endl;

	//通过区间构造
	vector<int> v2(v1.begin(),v1.end());
	cout << "v2 :";
	PrintVector(v2);
	cout << endl;

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

	//拷贝构造
	vector<int>v4(v3);
	PrintVector(v4);
	cout << endl;

}

void _01main()
{
	_01Test01();
}

vector容量和大小

功能:
对vector容器进行容量和大小的操作
函数原型:
empty(); //判断容器容量是否为空
capacity(); //容器的容量
size(); //返回容器中元素的个数
resize(int num); //重新指定容器的长度为num,若容器变长,则默认值0填充新的位置。如果容器变短,则末尾 超出容器长度的元素被删除。
resize(int num, elem); //重新指定容器的长度为num, 若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器长度的元素被删除

示例:

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

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

void _03Test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	PrintMyVecytor(v);

	//判断容器是否为空
	if (v.empty())
	{
		cout << "容器为空" << endl;
	}
	else {
		cout << "容器不为空" << endl;
		cout << "容器大小为:" << v.size() << endl;
		cout << "容器容量为:" << v.capacity() << endl;
	}

	//重新指定大小
	v.resize(20);
	cout << "重新指定大小后,容器容量为:" << v.capacity() << endl;

	//重新指定大小并填入elem
	v.resize(25, 2);
	PrintMyVecytor(v);
	cout << "重新指定大小并填入elem,容器大小为:" << v.size() << endl;
	cout << "重新指定大小并填入elem,容器容量为:" << v.capacity() << endl;
}

void main()
{
	_03Test01();
}

vector插入和删除

功能:
对vector容器进行插入、删除操作

函数原型:
push_back(elem); //尾插元素elem
pop_back(); //尾删元素
insert(const_iterator pos,elem); //迭代器指向位置pos插入count个元素elem
inser(const_iterator pos, int count, elem) //迭代器指向位置pos插入count个元素elem
erase(const_iterator pos); //删除迭代器指向的元素
erase(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
clear(); //删除容器中的所有元素

示例:

#include<iostream>
using namespace std;
#include<vector>
//对容器vector进行插入和删除操作
//push_back(elem);    //尾插元素elem
//pop_back();    //尾删元素
//insert(const_iterator pos, elem);    //迭代器指向位置pos插入count个元素elem
//inser(const_iterator pos, int count, elem)    //迭代器指向位置pos插入count个元素elem
//erase(const_iterator pos);    //删除迭代器指向的元素
//erase(const_iterator start, const_iterator end);    //删除迭代器从start到end之间的元素
//clear();    //删除容器中的所有元素
void MyShowVec(vector<int>& v)
{
	if (v.empty())
	{
		cout << "容器为空" << endl;
		return;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}

void _04Test01()
{
	//尾插
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	//遍历
	MyShowVec(v);

	//尾删
	v.pop_back();
	MyShowVec(v);

	//插入 第一个参数是迭代器
	v.insert(v.begin()+1,100);
	MyShowVec(v);

	//删除 参数也是迭代器
	v.erase(v.begin() + 1);
	MyShowVec(v);

	//清空容器
	v.clear();
	MyShowVec(v);

}

void main()
{
	_04Test01();

}

vector数据存取

功能:
对vector中的数据进行存取操作
函数原型:
at(int index); //返回索引index所指的数据
operator[](int index); //返回索引index所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素

示例:

#include<iostream>
using namespace std;
#include<vector>
//数据存取
//at(int index);    //返回索引index所指的数据
//operator[](int index);    //返回索引index所指的数据
//front();    //返回容器中第一个数据元素
//back();    //返回容器中最后一个数据元素
void MyShowVector(vector<int>& v)
{
	if (v.empty())
	{
		cout << "容器为空" << endl;
		return;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
void _05Test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	MyShowVector(v);

	//利用[]下标方式来访问数组的元素
	cout << "利用下标访问: " ;
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << "  ";
	}
	cout << endl;

	//利用at方式来访问元素
	cout << "利用at方式来访问元素: ";
	for (int i = 0; i < v.size(); i++)
	{
		cout << v.at(i) << "  ";
	}
	cout << endl;

	//获取第一个元素
	cout << "第一个元素为:" << v.front() << endl;

	//获取最后一个元素
	cout << "最后一个元素为: " << v.back() << endl;

}

void main()
{
	_05Test01();
}

vector互换容器

功能:
实现两个容器内元素互换

函数原型:
swap(vec); //将vec与本身的元素互换

示例:

#include<iostream>
using namespace std;
#include<vector>
//vector容器互换
//swap(vec);   //将vec与本身的元素互换
void MyPrintVec(vector<int>& v)
{
	if (v.empty())
	{
		cout << "容器为空" << endl;
		return;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}

void _06Test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	cout << "v1:  ";
	MyPrintVec(v1);

	vector<int> v2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	cout << "v2:  ";
	MyPrintVec(v1);

	v1.swap(v2);
	cout << "交换后:" << endl;
	cout << "v1:  ";
	MyPrintVec(v1);
	cout << "v2:  ";
	MyPrintVec(v2);
}

//利用swap收缩容量
void _06Test02()
{
	vector<int> v1;
	for (int i = 0; i < 10000; i++)
	{
		v1.push_back(i);
	}
	cout << "v1的容量为: " << v1.capacity() << endl;
	cout << "v1的大小为: " << v1.size() << endl;

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

	//利用swap进行空间收缩
	//(v1)为匿名对象 利用v1目前所用的元素来初始化匿名对象 再与匿名对象进行容器交换
	vector<int>(v1).swap(v1);
	cout << "利用swap进行空间收缩后:" << endl;
	cout << "v1的容量为: " << v1.capacity() << endl;
	cout << "v1的大小为: " << v1.size() << endl;

}

void main()
{
	//_06Test01();
	_06Test02();
}

vector预留空间

功能:
减小vector在动态扩展容量时的扩展次数

函数原型:
reserve(int len); //容器预留len个元素长度,预留位置不初始化,元素不可访问
示例:

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

//vector预留空间
//reserve(int len);     //容器预留len个元素长度,预留位置不初始化,元素不可访问
void _07Test01()
{
	//利用reserve与预留空间
	vector<int>v;
	v.reserve(100000);

	int num = 0;//记录空间开辟次数  不预留空间的情况下一共开辟了30次 预留后只开辟一次
	int* p = NULL;
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);
		if (p != &v[0])
		{
			p = &v[0];
			num++;
		}
	}
	cout << "num= "<<num << endl;
}

void main()
{
	_07Test01();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值