当前位置:   article > 正文

c++ vector拷贝(复制)方法总结_vector copy

vector copy

一、拷贝初始化
二、vector::assign
三、vector::swap
四、vector::insert
五、std::copy

//by 鸟哥 vector 拷贝的三种方式
//有疑问请留言或加群 1032082534
#include<iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& vec)
{
	for(auto val:vec)
	{
		cout<<val<<" ";
	}
	cout<<endl;
	return;
}
int main()
{
    std::vector<int> vec{11,22,33,44,55};
    //1、拷贝初始化
    cout<<"1、拷贝初始化:"<<endl;
    cout<<"源vector:";
    printVector(vec);
    std::vector<int> copyInit(vec);
    //vec[0]=999;//改变原vector的内容并不会修改拷贝后的vector的内容。
    cout<<"初始化后"<<endl;
    cout<<"源vector:";
    printVector(vec);
    cout<<"目标vector:";
    printVector(copyInit);
    
    //2、assign
    cout<<"2、assign:"<<endl;
    vector<int> assignVec;
    cout<<"源vector:";
    printVector(vec);
    assignVec.assign(vec.begin(), vec.end());
    cout<<"调用assign后"<<endl;
    cout<<"源vector:";
    printVector(vec);
    cout<<"目标vector:";
    printVector(assignVec);
    
    //3、swap
    cout<<"3、swap:"<<endl;
    cout<<"源vector:";
    printVector(vec);
    vector<int> swapVec;
    swapVec.swap(vec);
    cout<<"调用swap后"<<endl;
    cout<<"源vector:";
    printVector(vec);//这里输出为空,因为swap操作后,原来的vector就被清空了
    cout<<"目标vector:";
    printVector(swapVec);
    
    //4、insert
    cout<<"4、insert:"<<endl;
    cout<<"源vector:";
    printVector(swapVec);
    vector<int> insertVec;
    insertVec.insert(insertVec.end(), swapVec.begin(), swapVec.end());
    cout<<"调用insert后"<<endl;
    cout<<"源vector:";
    printVector(swapVec);
    cout<<"目标vector:";
    printVector(insertVec);
    
    //5.1、copy
    cout<<"5.1、copy方式1:"<<endl;
    vector<int> copyVec1;
     cout<<"源vector:";
    printVector(insertVec);
    copy(insertVec.begin(), insertVec.end(), std::back_inserter(copyVec1));
    cout<<"调用copy后"<<endl;
    cout<<"源vector:";
    printVector(insertVec);
    cout<<"目标vector:";
    printVector(copyVec1);
    //5.2、copy
    cout<<"5.2、copy方式2:"<<endl;    
    vector<int> copyVec2;
    cout<<"源vector:";
    printVector(insertVec);
    copyVec2.resize(5);//这里要预留空间,否则会出错
    copy(insertVec.begin(), insertVec.end(),copyVec2.begin());
    cout<<"调用copy后"<<endl;
    cout<<"源vector:";
    printVector(insertVec);
    cout<<"目标vector:";
    printVector(copyVec2);

    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

运行结果:

1、拷贝初始化:
源vector:11 22 33 44 55
初始化后
源vector:11 22 33 44 55
目标vector:11 22 33 44 55
2、assign:
源vector:11 22 33 44 55
调用assign后
源vector:11 22 33 44 55
目标vector:11 22 33 44 55
3、swap:
源vector:11 22 33 44 55
调用swap后
源vector:
目标vector:11 22 33 44 55
4、insert:
源vector:11 22 33 44 55
调用insert后
源vector:11 22 33 44 55
目标vector:11 22 33 44 55
5.1、copy方式1:
源vector:11 22 33 44 55
调用copy后
源vector:11 22 33 44 55
目标vector:11 22 33 44 55
5.2、copy方式2:
源vector:11 22 33 44 55
调用copy后
源vector:11 22 33 44 55
目标vector:11 22 33 44 55

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/452784
推荐阅读
相关标签
  

闽ICP备14008679号