標籤:c++ stl
由於好奇STL中的vector 對於自訂資料類型的 “ = ”(賦值運算子的)支援,謝了一段簡單的測試代碼進行測試。
結果證明vector對於賦值預算符支援良好,但是對於動態分配的類構成的vector數組,
博主認為一定要重寫解構函式與複製建構函式以及運算子多載“=”運算子(這是一條軟體規則,詳見博主測試),連結如下:
http://blog.csdn.net/u010003835/article/details/47314811
測試代碼:
#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中對 “=”賦值運算子的支援