C++/STL中 vector中對 “=”賦值運算子的支援

來源:互聯網
上載者:User

標籤: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中對 “=”賦值運算子的支援

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.