简体   繁体   English

C ++中的内置环形计数器

[英]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.我需要一个从零计数到 n 并在达到 n 时再次重置为 0 的环形计数器。 Now I know how to implement that myself, using the modulo operator.现在我知道如何使用模运算符自己实现它。 But is there a built-in or an std datatype for this?但是是否有内置的或标准的数据类型呢?

The mod operator is fundamental to C++ and other languages. mod 运算符是 C++ 和其他语言的基础。 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,我能想到的 C++ 中没有任何内置类型支持这种类型的操作,仅仅是因为 mod 操作符使用起来非常简单,

++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.正如上面的评论所说,如果你真的需要一个 C++ 类型,你可以自己制作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM