C++/STL中 vector中对 “=”赋值运算符的支持

由于好奇STL中的vector 对于自定义数据类型的 “ = ”(赋值运算符的)支持,谢了一段简单的测试代码进行测试。

结果证明vector对于赋值预算符支持良好,但是对于动态分配的类构成的vector数组,

博主认为一定要重写析构函数与复制构造函数以及运算符重载“=”运算符(这是一条软件规则,详见博主测试),链接如下:

测试代码:

#include <iostream>#include <vector>using namespace std;#pragma warning(disable:4996)struct Point{int x;int y;};void Print(vector<Point> &PointList){for (int i = 0; i < PointList.size(); i++){cout << PointList[i].x <<" "<< PointList[i].y << endl;}cout << endl << endl;}int main(){vector<Point> PointList1;vector<Point> PointList2;Point a;a.x = 1;a.y = 2;PointList1.push_back(a);a.x = 3;a.y = 2;PointList1.push_back(a);a.x = 4;a.y = 2;PointList1.push_back(a);Print(PointList1);cout << "test of = operator (whole vector copy)" << endl;PointList2 = PointList1;Print(PointList2);cout << "test of = operator (part of the vector)" << endl;PointList2[1] = PointList1[2];PointList2[0] = PointList1[2];Print(PointList2);return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

,那段雨骤风狂。人生之旅本就是风雨兼程,是要说曾经拥有,

C++/STL中 vector中对 “=”赋值运算符的支持

相关文章:

你感兴趣的文章:

标签云: