STL : copy a vector into another vector in C++ using copy


 By using inbuilt copy functions

copy(itBegin(),it!=end(), back_inserter()) :- first way to copy old vector into new one. This function takes 3 arguments, first, the first iterator is old vector first value, second, the last iterator of old ,Last in back_inserter. Vector V2 in empty ,so we can not use V2.begin() as a target but we have to use intserter.

 

First vector size: 5
First vector size: 5
Second vector size :5
--------------First Vector-------
1 2 3 4 5
--------------Second Vector-------
1 2 3 4 5
If your vector in pre initilized by some random value then you can use same above copy commend with V2.begin() inside copy.

First vector size: 5
First vector size: 5
Second vector size :5
--------------First Vector-------
1 2 3 4 5
--------------Second Vector-------
1 2 3 4 5

0 Comments