c++中vector的=(赋值)操作是深复制

首先是原文http://www.cplusplus.com/reference/vector/vector/operator=/

Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.
附上自己的测试代码

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    vector<int> b;
    b.push_back(4);
    b.push_back(5);

    for (int i = 0; i < a.size(); i++)
    {
        cout << a.at(i) << endl;
    }
    for (int i = 0; i < b.size(); i++)
    {
        cout << b.at(i) << endl;
    }
    b = a;
    for (int i = 0; i < b.size(); i++)
    {
        cout<<b.at(i)<<endl;
    }
    a.at(0) = 666;
    for (int i = 0; i < b.size(); i++)
    {
        cout << b.at(i) << endl;
    }
    return 1;
}

测试结果这里写图片描述
结果就是vector中存在=操作,该操作是赋值操作,用一个vector覆盖另一个,且是深复制。

猜你喜欢

转载自blog.csdn.net/u013992365/article/details/74276103