您的位置:首页 > 编程语言 > C语言/C++

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

2017-07-03 21:04 197 查看
首先是原文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覆盖另一个,且是深复制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言