C++学习笔记(二十七):c++ 动态数组vector及优化

  • c++的动态数组vector是STL的内容,关于STL,有兴趣可自行网上搜索资料。本节主要介绍vector的基本内容以及vector的简单优化。
  • vector当超过数组最大范围,需要往里面添加新的元素时,会在内存中创建一个比上一个更大的数组,将上一个数组中的所有元素复制过来,然后删除旧的数组。
  • #include <iostream>
    #include <vector>
    
    struct Vertex
    {
    	float x, y, z;
    	Vertex(float x,float y, float z)
    	{
    		this->x = x;
    		this->y = y;
    		this->z = z;
    	}
    };
    
    std::ostream& operator<< (std::ostream& stream, const Vertex& vertex)
    {
    	stream << vertex.x << vertex.y << vertex.z;
    	return stream;
    }
    
    int main()
    {
    	std::vector<Vertex> vertices;
    	vertices.push_back(Vertex(1.3f, 2.1f, 4.4f));
    	vertices.push_back({ 2,3,4 });
    	//两种方式便利vertor
    	//for (int i = 0; i < vertices.size(); i++)
    	//{
    		//std::cout << vertices[i] << std::endl;
    	//}
    	for (Vertex &ver:vertices)  //传入引用的原因时为了尽量减少复制,如果不是引用,则会将每个vertices复制到for循环中,影响性能
    	{
    		std::cout << ver << std::endl;
    	}
    
    	vertices.erase(vertices.begin() + 1); //删除vertices中的第二个元素。因为erase中的参数时一个iterator
    	vertices.clear(); //清空整个vertices中的元素
    
    	std::cin.get();
    	return 0;
    }

    下面简单对vector进行优化

  • vector影响性能的主要原因是当插入新的元素是,原来的vector分配的剩余内存不够时,需要复制原来vector中存在的所有元素,重新分配内存,从而影响程序的性能。

  • #include <iostream>
    #include <vector>
    
    struct Vertex
    {
    	float x, y, z;
    	Vertex(float x,float y, float z)
    	{
    		this->x = x;
    		this->y = y;
    		this->z = z;
    	}
    	//新增一个拷贝构造函数,查看复制操作的产生时机
    	Vertex(const Vertex& ver)
    		:x(ver.x),y(ver.y),z(ver.z)
    	{
    		std::cout << "进行一次复制操作!!!" << std::endl;
    	}
    };
    
    std::ostream& operator<< (std::ostream& stream, const Vertex& vertex)
    {
    	stream << vertex.x << ", " << vertex.y<< ", " << vertex.z;
    	return stream;
    }
    
    int main()
    {
    	std::vector<Vertex> vertices;
    	vertices.push_back(Vertex(1.3f, 2.1f, 4.4f));
    	vertices.push_back({ 2,3,4 });
    	//这个状态下执行三次复制操作
    
    	std::cin.get();
    	return 0;
    }

    运行结果

  • ​​​​​​​

  • #include <iostream>
    #include <vector>
    
    struct Vertex
    {
    	float x, y, z;
    	Vertex(float x,float y, float z)
    	{
    		this->x = x;
    		this->y = y;
    		this->z = z;
    	}
    	//新增一个拷贝构造函数,查看复制操作的产生时机
    	Vertex(const Vertex& ver)
    		:x(ver.x),y(ver.y),z(ver.z)
    	{
    		std::cout << "进行一次复制操作!!!" << std::endl;
    	}
    };
    
    std::ostream& operator<< (std::ostream& stream, const Vertex& vertex)
    {
    	stream << vertex.x << ", " << vertex.y<< ", " << vertex.z;
    	return stream;
    }
    
    int main()
    {
    	std::vector<Vertex> vertices;
    	vertices.reserve(3);
    	//vertices.push_back(Vertex(1.3f, 2.1f, 4.4f));
    	//vertices.push_back({ 2,3,4 });
    	//vertices.push_back(Vertex(3, 4, 5));
    	//提前reserve后,这个状态下执行三次复制操作,是因为push_back操作,每次先在main函数的栈帧创建一个Vertex,然后再将main中创建好的Vertex复制到vertices分配好的内存中
    	vertices.emplace_back(1.3f, 2.1f, 4.4f);
    	vertices.emplace_back( 2,3,4 );
    	vertices.emplace_back(3, 4, 5);
    	//使用emplace_back替换push_back,这个状态下不会执行复制操作,因为push_back是用参数的数据在vertices分配好的内存中创建Vertex对象,不需要再去复制
    
    	std::cin.get();
    	return 0;
    }

  • 30
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值