-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path34_copy_if.cpp
More file actions
36 lines (30 loc) · 827 Bytes
/
34_copy_if.cpp
File metadata and controls
36 lines (30 loc) · 827 Bytes
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
/* output
vector copy if 6 count: 1
init list copy if 6 count: 1
*/
#include <range/v3/algorithm/copy_if.hpp>
#include <range/v3/utility/iterator.hpp>
#include <range/v3/algorithm/for_each.hpp>
namespace rng = ranges::v3;
#include <vector>
#include <iostream>
using namespace std;
auto is_six = [](int i) -> bool { return i == 6; };
auto print = [] (int i) { cout << i << " "; };
int main() {
vector<int> v { 1, 2, 3, 4, 5, 6};
{
// copy from one container to another
vector<int> v_cpy;
rng::copy_if(v, rng::back_inserter(v_cpy), is_six);
rng::for_each( v_cpy, print ); //6
cout << "\n";
}
{
// copy from braced init list range
vector<int> v_cpy;
rng::copy_if({6, 7, 8}, rng::back_inserter(v_cpy), is_six);
rng::for_each( v_cpy, print ); //6
cout << "\n";
}
}