栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

vector容器的基本用法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vector容器的基本用法

1.vector容器基本概念:
功能:vector数据结构和数组非常相似,也称为单端数组
vector与普通数组的区别:不同之处在于数组是静态空间,vector可以动态扩展
动态扩展:并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原有数据拷贝新空间,释放原空间.

2.vector常用函数:

push_back   在尾部插入数据
pop_back: 删除尾部数据
insert:插入数据
begin():迭代器第一个元素
 end():迭代器指向最后一个元素后一个位置
front():元素第一个元素

3.vector构造函数


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

代码如下:

void printVector(vector&v)//输入v进来
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";//解引用
	}
	cout << endl;
}
void test01()
{
	vectorv1;//1.默认构造 无参构造
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);//输入10个数进去
	}
	printVector(v1);
	//2.通过区间方式进行构造
	vectorv2(v1.begin(), v1.end());//把区间中的元素拷贝给v2
	printVector(v2);
	//3.n个elem构造
	vectorv3(10, 100);//个数 赋值
	printVector(v3);
	//拷贝构造
	vectorv4(v3);
	printVector(v4);
}




int main()
{
	test01();


	return 0;
}

4.vector赋值操作

代码如下:

#include
#include
using namespace std;
//vector赋值
void print(vector&v)//&表示引用
{
	for (vector::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vectorv1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	print(v1);
	//赋值 1. operator=
	vectorv2;
	v2 = v1;
	print(v2);
	//2.assign方式
	vectorv3;
	v3.assign(v1.begin(), v1.end());
	print(v3);
	//3.n个elem方式赋值
	vectorv4;
	v4.assign(10, 100);
	print(v4);
}



int main()
{
	test01();





}

5.vector容量和大小

代码如下:

#include
#include
using namespace std;
//empty()判断容器是否为空
//capacity()容量
//size 大小
void print(vector& v) {
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it <<" ";
	}
	cout << endl;
}
void test01()
{
	vectorv1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

	print(v1);
	if (v1.empty()) {//为真 代表容器为空 
		cout << "v1为空" << endl;
	}
	else {
		cout << "v1不为空" << endl;
		cout << "v1的容量:" << v1.capacity() << endl;
		cout << "v1的大小:" << v1.size() << endl;
	}
	//重新指定大小
	v1.resize(15,100);//利用重载版本,可以指定默认填充值,参数2
	print(v1);//如果重新指定的比原来长了,默认用0填充新的位置
	v1.resize(5);
	print(v1);//如果重新指定的比原来短了,超出位置删掉
}






int main()
{

	test01();


}

6.vector插入和删除

代码如下:

#include
#include
using namespace std;
//vector插入和删除
void print(vector& v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vectorv1;
	//尾插法
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	print(v1);
	//尾删
	v1.pop_back();//尾删 删掉最后一个
	print(v1);

	//插入
	v1.insert(v1.begin(), 100);//提供一个迭代器进行插入
	print(v1);
	v1.insert(v1.begin(), 2, 1000);在初始位置插入两个1000
	print(v1);
	//删除  参数也是迭代器
	v1.erase(v1.begin());//删掉起始值
	print(v1);
	//清空
	//v1.erase(v1.begin(), v1.end());
	v1.clear();//也是清空
	print(v1);//空容器
}



int main()
{
	test01();




}

7.vector互换容器

#include
#include
#include
using namespace std;
void print(vector& v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//vector容器互换
//1.基本使用
void test01()
{
	vectorv1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	cout << "交换前:" << endl;
	print(v1);
	vectorv2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	print(v2);
	cout << "交换后:" << endl;
	v1.swap(v2);//swap
	print(v1);
	print(v2);
}
//2.实际用途
//巧用swap可以收缩内存空间
void test02()
{
	vectorv;
	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(v).swap(v);//vector(v)是匿名对象
	cout << "v的容量:" << v.capacity() << endl;
	cout << "V的大小:" << v.size() << endl;
}


int main()
{

	//test01();
	test02();


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号