赋值操作
函数原型:
代码语言:javascript复制#include<iostream>
using namespace std;
#include<vector>
//vector赋值操作
void print(vector<int>& v)
{
for (vector<int>::iterator beg = v.begin(); beg != v.end(); beg )
{
cout << *beg <<" ";
}
cout << endl;
}
void test()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
print(v);
//1.重载等号运算符 operator=
vector<int> v1;
v1 = v;
print(v1);
//2.[beg,end)区间赋值拷贝给本身
vector<int> v2;
v2.assign(v1.begin(), v1.end());
print(v2);
//3.将n个elem拷贝赋值给本身
vector<int> v3;
v3.assign(5, 20);
print(v3);
}
int main()
{
test();
system("pause");
return 0;
}