-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path32_copy.cpp
50 lines (38 loc) · 1.11 KB
/
32_copy.cpp
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
/* output
vector: { 1 2 3 4 5 6 7 8 }
vector: { 11 12 3 4 5 6 7 8 9 10 }
*/
#include <range/v3/view/iota.hpp>
#include <range/v3/algorithm/copy.hpp>
#include <range/v3/algorithm/for_each.hpp>
#include <range/v3/utility/iterator.hpp>
namespace rng = ranges::v3;
#include <vector>
#include <iostream>
using namespace std;
auto print = [] (int i) { cout << i << " "; };
void print_vec(const vector<int>& v)
{
cout << "vector: { ";
rng::for_each(v, print);
cout << "}\n";
}
int main() {
vector<int> v { 1, 2, 3, 4, 5 };
// copy from one container to another
vector<int> v_cpy;
rng::copy(v, rng::back_inserter(v_cpy));
assert( v == v_cpy );
// copy from braced init list range
rng::copy({6, 7, 8}, rng::back_inserter(v_cpy));
assert( v_cpy.size() == 8 );
print_vec( v_cpy );
// you can copy from ranges ("ints" gives a "half open" one,
// i.e. just 9 and 10):
rng::copy(rng::view::ints(9, 11), rng::back_inserter(v_cpy));
assert( v_cpy.size() == 10 );
// you can overwrite as well
rng::copy(rng::view::ints(11, 13), v_cpy.begin());
assert( v_cpy.size() == 10 );
print_vec( v_cpy );
}