赞
踩
vector包含在c++标准模版库中,能够存放任意类型的动态数组,类似于一个容器,能够增加数据和减少数据
使用方法如下:
- #include <iostream>
- #include <vector>
-
- struct Vertex{
- int x,y,z;
-
- Vertex(int a, int b, int c)
- : x(a), y(b),z(c){}
- };
-
- std::ostream& operator<<(std::ostream& stream, const Vertex& vertex){
- stream << vertex.x << "," << vertex.y << "," << vertex.z;
- return stream;
- }
-
- int main(){
- std::vector<Vertex> vertices;
- Vertex v1(1,2,3);
- Vertex v2(4,5,6);
- vertices.push_back(v1);
- vertices.push_back(v2);
-
- for(int i = 0; i< vertices.size(); i++){
- std::cout << vertices[i] <<std::endl;
- }
-
- vertices.erase(vertices.begin() +1);
-
- for(Vertex& v : vertices){
- std::cout << v << std::endl;
- }
-
- std::cin.get();
- }
1. #include <vector> 声明头文件
2. std::vector<Vertex> vertices; 创建动态数组(数组类型为Vertex)
3. vertices.push_back(v1); 将Vertex类型的变量v1加入数组
4. vertices.erase(vertices.begin() +1); 删除第二个位置的值
当往vector数组添加元素时,会发生多次没必要的拷贝,降低代码效率,如下:
- #include <iostream>
- #include <vector>
-
- struct Vertex{
- int x,y,z;
-
- Vertex(int a, int b, int c)
- : x(a), y(b),z(c){}
-
- Vertex(const Vertex& vertex)
- :x(vertex.x), y(vertex.y), z(vertex.z){
- std::cout << "copy" << 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;
-
- Vertex v1(1,2,3);
- Vertex v2(4,5,6);
- Vertex v3(4,5,6);
- vertices.push_back(v1);
- vertices.push_back(v2);
- vertices.push_back(v3);
-
- std::cin.get();
- }
当动态数组创建时,因为没有包含元素,所以不占用内存。当需要push元素进数组时,系统会调整数组长度为1,然后拷贝该元素到数组。当再次push第二个元素时,系统会调整数组长度为2,而且会指定一个新的内存地址,然后把两个元素都拷贝过去。这就降低了效率。同理,第三个元素时,拷贝次数到了6次。
- $ ./a.out
- copy
- copy
- copy
- copy
- copy
- copy
为了应对该问题,分两步:
1. 先为该数组预留长度,这样只要数组实际长度没达到这个值,就不会发生上述现象。代码如下:
vertices.reserve(3);
- #include <iostream>
- #include <vector>
-
- struct Vertex{
- int x,y,z;
-
- Vertex(int a, int b, int c)
- : x(a), y(b),z(c){}
-
- Vertex(const Vertex& vertex)
- :x(vertex.x), y(vertex.y), z(vertex.z){
- std::cout << "copy" << 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);
-
- Vertex v1(1,2,3);
- Vertex v2(4,5,6);
- Vertex v3(4,5,6);
- vertices.push_back(v1);
- vertices.push_back(v2);
- vertices.push_back(v3);
-
-
- std::cin.get();
- }
运行结果显示,只发生了三次拷贝
- $ ./a.out
- copy
- copy
- copy
2. 进一步优化,即不在main函数中创建元素再拷贝到数组,而是直接在预留的内存地址生成,代码如下
- vertices.emplace_back(1,2,3);
- vertices.emplace_back(4,5,6);
- vertices.emplace_back(7,8,9);
- #include <iostream>
- #include <vector>
-
- struct Vertex{
- int x,y,z;
-
- Vertex(int a, int b, int c)
- : x(a), y(b),z(c){}
-
- Vertex(const Vertex& vertex)
- :x(vertex.x), y(vertex.y), z(vertex.z){
- std::cout << "copy" << 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);
-
- // Vertex v1(1,2,3);
- // Vertex v2(4,5,6);
- // Vertex v3(4,5,6);
- // vertices.push_back(v1);
- // vertices.push_back(v2);
- // vertices.push_back(v3);
- vertices.emplace_back(1,2,3);
- vertices.emplace_back(4,5,6);
- vertices.emplace_back(7,8,9);
-
- std::cin.get();
- }
运行结果显示一次拷贝也没有发生。
注意在mac上编译时,如果使用xcode工具链的编译工具,会报错
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。