Skip to content

Commit 337c28c

Browse files
copy_n
1 parent 5e7043a commit 337c28c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

vector_copy_part.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <vector>
2+
#include <algorithm> //copy, copy_n
3+
#include <iterator> //advance
4+
#include <iostream>
5+
using namespace std;
6+
7+
//https://stackoverflow.com/questions/13760174/replacing-subvector-in-a-vector
8+
9+
int main() {
10+
vector<int> vec1 = {1,2,3,4};
11+
vector<int> vec2 = {5,6,7,8,9,10};
12+
13+
int n = 1;
14+
vector<int>::iterator dst = vec2.begin();
15+
advance(dst, n);
16+
17+
//Method 1: copy
18+
copy(vec1.begin(), vec1.begin()+3, dst);
19+
20+
//Method 2: copy_n
21+
copy_n(begin(vec1), 3, dst);
22+
23+
for(int e : vec2){
24+
cout << e << " ";
25+
}
26+
cout << endl;
27+
28+
return 0;
29+
}
30+
31+
//5 1 2 3 9 10

0 commit comments

Comments
 (0)