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

相关推荐

  1. C++学习笔记

    2024-01-09 19:32:03       30 阅读
  2. C++学习笔记

    2024-01-09 19:32:03       29 阅读
  3. C++学习笔记

    2024-01-09 19:32:03       27 阅读
  4. C++学习笔记(三):c++ 计时

    2024-01-09 19:32:03       31 阅读
  5. C#(C Sharp)学习笔记_继承【

    2024-01-09 19:32:03       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 19:32:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 19:32:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 19:32:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 19:32:03       18 阅读

热门阅读

  1. 【MySQL】ANY函数 的巧用(筛选字段 = ANY(语句))

    2024-01-09 19:32:03       39 阅读
  2. 逆流而上-摘抄句子

    2024-01-09 19:32:03       36 阅读
  3. Git命令 本地-远程 简洁步骤

    2024-01-09 19:32:03       31 阅读
  4. React-路由进阶

    2024-01-09 19:32:03       33 阅读