C++动态数组,Vector的基本使用

 一、实际例子

#include<iostream>
#include<string>
#include<vector>
struct Vertex
{
	float x, y, z;
};

std::ostream& operator<<(std::ostream& stream, const Vertex& vertex)
{
	stream << vertex.x << "," << vertex.y << "," << vertex.z;
	return stream;
}

void Function(const std::vector<Vertex>& vertices)//在函数中传递数组一般需要&,因为要确保不是将整个数组都复制到其中
{

}
int main()
{

	std::vector<Vertex> vertices;//动态数组
	vertices.push_back({ 1,2,3 });//加入元素
	vertices.push_back({ 4,5,6 });
	Function(vertices);

	for (int i = 0; i < vertices.size(); i++)//遍历动态数组
		std::cout << vertices[i] << std::endl;

	for (Vertex v : vertices)//遍历动态数组的另外一种形式,这实际上是将每个vertex复制到这个for循环当中,我们一般不想这样做
		std::cout << v << std::endl;

	for (Vertex& v : vertices)//加上&就不会把东西复制过来
		std::cout << v << std::endl;

	vertices.clear();//清除动态数组

	vertices.erase(vertices.begin() + 1);//清除第二个元素
	std::cin.get();
}

注:Vector本身就很慢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值