Recommand · March 21, 2022 0


How to iterate over a range-v3 action?

I’m new to range-v3. I started by write a program that generates a range of integers, modifies it, and prints out the resulting range. I used a modifier from the actions library, which rendered my code non-iterable. Could someone help me understand how to convert a ranges::action::action_closure into an iterable?

Here’s the example I started with – a function that generates and prints out a range of numbers. This code works fine and compiles.

void generate_and_print_numbers_using_streams_and_iterators() {
  auto v = views::iota(1, 5);  // <-- this code works fine with streams and iterators.

  // this works
  cout << v << endl;

  //this works too
  for (auto s: v) {
    cout << s << ", ";
  }
  cout << endl;

  // and this works too
  for (auto it = v.begin(); it != v.end(); ++it) {
    cout << *it << ", ";
  }
  cout << endl;
}

Then I tried introducing the action I mentioned. This code no longer compiles – v is no longer a view, but an action_closure which does not define begin() and end() functions.

void generate_and_print_numbers_using_streams_and_iterators() {
  // I REPLACED THIS:
  // auto v = views::iota(1, 5);
  // WITH THIS:
  auto v = action::push_back(views::iota(1, 5)) | actions::unique;  // Wrapped iota in push_back

  // .. the remainder of the function is unchanged

I tried searching through the documentation of ranges-v3, as well as googling this conversion. The only thing I found was the following article that shows how to convert a view to a concrete container (a vector).

I will appreciate any help on this topic.




You may also like…




amazon-web-services android angular api arrays c# css dart dataframe django docker excel express firebase flutter html ios java javascript jquery json kotlin laravel linux list mongodb mysql node.js pandas php postgresql python python-3.x r react-native reactjs regex spring spring-boot sql sql-server string swift typescript vue.js