Using std::iota() with a Custom Step Size

How can I use std::ranges::iota() to generate a sequence with a custom step size?

To generate a sequence with a custom step size using std::ranges::iota(), you will need to create a custom range by combining std::views::iota with a std::views::transform view.

This approach allows you to apply a transformation to each element in the generated sequence.

Here's an example that generates a sequence with a step size of 2:

#include <iostream>
#include <ranges>
#include <vector>

int main() {
  auto custom_range = std::views::iota(1)
    | std::views::transform([](int n) {
      return n * 2;
    });

  // Take the first 10 elements
  std::vector<int> sequence(
    custom_range.begin(),
    custom_range.begin() + 10
  );

  for (int n : sequence) {
    std::cout << n << ", ";
  }
}
2, 4, 6, 8, 10, 12, 14, 16, 18, 20,

In this example, std::views::iota(1) generates an infinite range starting from 1. The std::views::transform view applies a lambda function to each element, multiplying it by 2. The result is a sequence with a step size of 2.

You can also generate sequences with other custom step sizes by modifying the lambda function. For example, to generate a sequence with a step size of 3:

#include <iostream>
#include <ranges>
#include <vector>

int main() {
  auto custom_range = std::views::iota(1)
    | std::views::transform([](int n) {
    return n * 3;
  }); 

  // Take the first 10 elements
  std::vector<int> sequence(
    custom_range.begin(),
    custom_range.begin() + 10
  );

  for (int n : sequence) {
    std::cout << n << ", ";
  }
  std::cout << "\n";
}
3, 6, 9, 12, 15, 18, 21, 24, 27, 30,

By combining std::views::iota with std::views::transform, you can create sequences with any custom step size. This method is flexible and allows you to apply various transformations to the generated elements.

Using views in this way leverages the power of ranges to create complex sequences with minimal code, making your programs more readable and expressive.

8 Key Standard Library Algorithms

An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Using std::ranges::for_each() with a Member Function
How do I use std::ranges::for_each() with a member function of a class?
Comparing Only Part of Two Ranges
Can I use std::ranges::equal() to compare only a part of two ranges?
Merging Three or More Sorted Ranges
How do I merge three or more sorted ranges using std::ranges::merge()?
Ensuring No Repeated Elements in std::ranges::sample()
Can I use std::ranges::sample() to ensure no repeated elements in the sample?
Removing Duplicates from a Range
What is the most efficient way to remove duplicates from a range using standard algorithms?
Using Subranges vs Iterator Pairs
What are the use cases for using std::ranges::subrange() versus standard iterator pairs?
std::ranges::set_union() vs std::ranges::merge()
What are the benefits of using std::ranges::set_union() over std::ranges::merge() in terms of performance and use cases?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant