C++ Vector使用

C++ Vector使用

参考链接

初始化

传入 a[0]开始到a[6]

void CoutVector(std::vector<int> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}
	
	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+7);
	CoutVector(AVec);

将Avec赋值给BVec


void CoutVector(std::vector<int> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}

std::vector<int> BVec(AVec);
CoutVector(BVec);

把AVec[2]到AVec[5]赋值给Cvec

void CoutVector(std::vector<int> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}

	std::vector<int> CVec(AVec.begin()+2, AVec.begin() +5);
	CoutVector(CVec);

初始化 15个元素 元素数据为1

void CoutVector(std::vector<int> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}
std::vector<int> DVec(15,1);
	CoutVector(DVec);

初始化 15个元素 元素数据为0

void CoutVector(std::vector<int> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}
	
	std::vector<int> EVec(15);
	CoutVector(EVec);

遍历

迭代器遍历

void CoutVector(std::vector<int> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}


角标遍历

void CoutVector1(std::vector<int> AVec)
{
	for (int it = 0; it <AVec.size(); it++)
	{
		std::cout << AVec[it] << std::endl;
	}
}

赋值

	//初始化 传入 a[0]开始到a[6]
	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+4);
	std::vector<int> BVec(a+4, a + 7);

	//BVec为向量,将BVec的0-2个元素赋值给向量AVec 但是结果会覆盖AVec本身的数据
	AVec.assign(BVec.begin(), BVec.begin() + 3);
	//AVec含有5个值为3的元素 但是结果会覆盖AVec本身的数据
	AVec.assign(5, 3);

插入

在AVec的最后一个向量后插入一个元素

	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+4);
	std::vector<int> BVec(a+4, a + 7);
	AVec.push_back(5);
	CoutVector(AVec);

在AVec的某个位置插入数值,

	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+4);
	std::vector<int> BVec(a+4, a + 7);
	AVec.insert(AVec.begin() + 2, 5);

在AVec的某个位置连续插入多个相同数值

	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+4);
	std::vector<int> BVec(a+4, a + 7);
	AVec.insert(AVec.begin() + 1, 3, 5);

在AVec的某个位置连续插入另一个向量的数据

在AVec的某个位置起,插入Bevc的1的位置开始到2结束,并且不包含2

	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+4);
	std::vector<int> BVec(a+4, a + 7);
	AVec.insert(AVec.begin() + 1, BVec.begin() + 1, BVec.begin() + 2);
	CoutVector(AVec);

删除

删除最后一个元素

int a[] = { 1,2,3,4,5,6,7,8,9 };
std::vector<int> AVec(a,a+9);
AVec.pop_back();

删除从某一位开始,至某一位结束,但不包含结束位的连续数据

	int a[] = { 1,2,3,4,5,6,7,8,9 };
	std::vector<int> AVec(a,a+9);
AVec.erase(AVec.begin() + 1, AVec.begin() + 3);
CoutVector(AVec);

删除某个元素

AVec.erase(AVec.begin() + 1);
	CoutVector(AVec);

清空

int a[] = { 1,2,3,4,5,6,7,8,9 };
std::vector<int> AVec(a,a+9);
AVec.clear();
CoutVector(AVec);

元素获取

获取最后一个元素

	int i = AVec.back();

获取第一个元素

	int i1 = AVec.front();

按角标获取元素的值,角标值需要小于数组的最大个数

	int i2 = AVec[2];

数组个数

是否为空 空为真 非空为假

AVec.empty()

数组大小

AVec.size();

将现有元素个数调整至10个,多则删,少则补,其值随机

	AVec.resize(10);

将现有元素个数调整至10个,多则删,少则补,其值为2

	AVec.resize(10, 2);

将a的容量扩充至100

	AVec.reserve(100);

算法

交换算法 交换两个向量的值

	AVec.swap(BVec);

倒置算法:从某一位开始到结束调换位置,但不包含结束位置

	reverse(AVec.begin(), AVec.end());

从小到大排序:从某一位开始到结束位置,但不包含结束位置 ,不适用于自己创建的结构体

sort(AVec.begin(), AVec.end());

复制算法: 把a中的从AVec.begin()到AVec.end()的元素结束,不包含结束位,复制到BVec中,从BVec.begin()+1的位置开始复制,覆盖掉原有元素,但复制的个数不可超过Bvec的结束位置

	copy(AVec.begin(), AVec.end(), BVec.begin() + 1);

重写排序算法

原文

当使用自定义结构体时,需要重写sort的排序算法实现想要的排序结果

方法1

struct AAA
{
	AAA(int A)
	{
		a = A;
	}
	int a;
};
void CoutVector(std::vector<AAA> AVec)
{
	for (auto it = AVec.begin(); it != AVec.end(); it++)
	{
		AAA A = *it;
		std::cout << A.a << std::endl;
	}
}

void CoutVector1(std::vector<AAA> AVec)
{
	for (int it = 0; it <AVec.size(); it++)
	{
		AAA A = AVec[it];
		std::cout << A.a << std::endl;
	}
}
#include<algorithm>


//如果返回的值为true 则把第一个数放到前面,false为把第二个值放到前面
bool comp(AAA a, AAA b)
{
	return a.a > b.a;
}

int main()
{

	int AAAAAAA = 6>5;
	std::cout << AAAAAAA << std::endl;
	//初始化 传入 a[0]开始到a[6]
	AAA a[] = { AAA(1),AAA(2),AAA(3),AAA(4),AAA(6),AAA(5),AAA(7),AAA(8),AAA(9) };
	std::vector<AAA> AVec(a,a+9);
	sort(AVec.begin(), AVec.end(), comp);
	CoutVector(AVec);
	system("pause");
    return 0;
}

方法二

原文出处

#include   <vector>
#include   <algorIThm>
#include <functional>
using   namespace   std;
struct AssessTypeInfo
{
	unsigned int m_uiType;   //类型ID
	char   m_szName[64];  //类型名称
	unsigned int m_uiTotal;   //总分数
	bool   operator <  (const   AssessTypeInfo&   rhs)  const   //升序排序时必须写的函数
	{
		return   m_uiType   <   rhs.m_uiType;
	}
	bool   operator >  (const   AssessTypeInfo&   rhs)  const   //降序排序时必须写的函数
	{
		return   m_uiType   >   rhs.m_uiType;
	}
};
int   main()
{
	vector<AssessTypeInfo >   ctn;
	AssessTypeInfo a1;
	a1.m_uiType = 1;
	AssessTypeInfo  a2;
	a2.m_uiType = 2;
	AssessTypeInfo  a3;
	a3.m_uiType = 3;
	ctn.push_back(a1);
	ctn.push_back(a2);
	ctn.push_back(a3);
	//升序排序
	sort(ctn.begin(), ctn.end(), less<AssessTypeInfo>());   //或者sort(ctn.begin(), ctn.end())  默认情况为升序

	//降序排序
	sort(ctn.begin(), ctn.end(), greater<AssessTypeInfo>());

	system("pause");
	return   0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值