std::iota and custom incrementable

Definition

Fills the range [first, last) with sequentially increasing values, starting with value and repetitively evaluating ++value.

 

Syntax

But now let’s create a custom incrementable class. Iota is working with a T type, so we only need to overload the ++ operator.

So what do we have here?

  • I’ve added a templated class that defines a function that receives a const T& and returns another T. This will be used to increment the value. (or whatever we want to do with it).
  • We send the initial value via constructor, and the callable function. The function is called in the operator++ function, to fetch the newly incremented value.
  • When we create the Incrementable object, we send it a lambda that receives an int (because that’s our vector data type) and returns that value + 3.

You may also like...

Leave a Reply