Oblomov
Oblomov

Reputation: 9635

Built-in ring-counter in c++

I need a ring-counter that counts from zero to n and resets to 0 again when n is reached. Now I know how to implement that myself, using the modulo operator. But is there a built-in or an std datatype for this?

Upvotes: 0

Views: 1007

Answers (1)

Jayson Boubin
Jayson Boubin

Reputation: 1504

The mod operator is fundamental to C++ and other languages. This is definitely the best operator to use. There aren't any built-in types in C++ that I can think of that support this type of operation, simply because of how easy the mod operator is to use,

++count %= n;

For example, This simple piece of code increments and mods a number in one line.

As the comment above says, if you really need a type in C++ you can just make it yourself.

Upvotes: 2

Related Questions