Fluent::Programmer

    Home Blog About
  • Home
  • Blog
  • About

Beautiful code #6 - std::copy_if, std::remove_copy_if, std::copy, std::execution::par_unseq, std::back_inserter πŸ‘‹

  • Fluent Programmer
  •   3 min read

Quick summary ↬  std::copy_if, std::remove_copy_if, std::copy, std::back_inserter, and std::execution::par_unseq are features from C++11 and C++98 that helps efficiently copy from one data structure to the another.

  1. std::execution introduced in C++17
  2. std::copy_if introduced in C++11
  3. std::remove_copy_if introduced in C++11
  4. std::back_inserter introduced in C++98
  5. std::copy introduced in C++98

Beautiful code #6

 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
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <execution>


/***********************************************************
          std::execution introduced in C++17
          std::copy_if introduced in C++11
          std::remove_copy_if introduced in C++11
          std::back_inserter introduced in C++98
          std::copy introduced in C++98
***********************************************************/

int main() {
    std::vector<int> data{1,2,3,4,5,6,7,8};
    std::vector<int> copy_if_vec;
    
    std::copy_if(data.begin(), 
                 data.end(), 
                 std::back_inserter(copy_if_vec),
                 [](int v) { return v % 2 == 0;});
    
    for(auto &v : copy_if_vec) {
        std::cout << v << " ";
    }
    std::cout << "\n";
    
    
    std::vector<int> remove_copy_if_vec;
    
    std::remove_copy_if(data.begin(), 
                        data.end(),
                        std::back_inserter(remove_copy_if_vec),
                        [](int v) { return v % 2 == 0;});
    
    for(auto &v : remove_copy_if_vec) {
        std::cout << v << " ";
    }
    std::cout << "\n";
    
    
    std::vector<int> copy_vec;
    
    std::copy(data.begin(),
              data.end(),
              std::back_inserter(copy_vec)); // copy_vec.begin() also works.
    
    for(auto &v : copy_vec) {
        std::cout << v << " ";
    }
    std::cout << "\n";
    
    std::vector<int> par_exec_vec;
    std::copy_if(std::execution::par, data.begin(), 
                 data.end(), 
                 std::back_inserter(par_exec_vec),
                 [](int v) { return v % 2 == 0;});
                 
    for (auto &v : par_exec_vec) {
        std::cout << v <<  " ";
    }
    std::cout << "\n";
}

Beautiful code #6 explanation

1
2
3
4
std::copy_if(data.begin(), 
                 data.end(), 
                 std::back_inserter(copy_if_vec),
                 [](int v) { return v % 2 == 0;});

std::copy_if accepts 4 arguments - The first one is an iterator that points to the beginning value of the vector. The second one is an iterator that points to the end value of the vector. The third one is a std::back_inserter that accepts a vector to which it needs to copy the elements that satisfies the condition given in the argument 4. In our example, that is a lambda function to check the number is an even number.

1
2
3
4
std::remove_copy_if(data.begin(), 
                        data.end(),
                        std::back_inserter(remove_copy_if_vec),
                        [](int v) { return v % 2 == 0;});

std::remove_copy_if is something similar to std::copy_if but the only difference is it copies the elements that don’t satisfy the condition given in the lambda function.

std::execution::par is just used to tell parallel copy is allowed. This is newly introduced in C++17.

You will also like β€” More Articles

https://fluentprogrammer.com/beautiful-code-1-using-define-templates-r-value-reference/ https://fluentprogrammer.com/beautiful-code-2-enable_if_t-template-inside-a-template/ https://fluentprogrammer.com/beautiful-code-3-no_unique_address_cpp_20-feature/ http://fluentprogrammer.com/beautiful-code-4-any_of-none_of-all_of/ https://fluentprogrammer.com/beautiful-code-5-for_each-optional/

You may like this

About The Author

Fluentprogrammer doesn't need coffee to program. They run on pure caffeine and lines of code.

Email Newsletter

Table of Contents

  • Beautiful code #6
  • Beautiful code #6 explanation
  • You will also like β€” More Articles
  • You may like this
  • C++
  • Beautiful code series

Unhealthy love with dark corners of C++

Founded by an engineer to help engineers. 2021–2023.

  • About us
  • Privacy policy