Thanks to the curiosity of vectors in the STL for the "=" (Assignment operator) support for custom data types, a simple test code was tested.
It turns out that vectors support good for assignment budgeting, but for vector arrays of dynamically allocated classes,
Bloggers think it's important to override the destructor with the copy constructor and the operator overload "=" operator (this is a software rule, see the blogger test), linked as follows:
http://blog.csdn.net/u010003835/article/details/47314811
Test code:
#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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Support for the "=" assignment operator in vectors in C++/stl