c++ - 跟踪 boolean 数据

标签 c++ list boolean bit

我需要跟踪 n 个样本。我跟踪的信息是 boolean 类型的,即是真还是假。一旦我在样本 n+1 上,我基本上想忽略最旧的样本并记录有关最新样本的信息。

所以说我跟踪样本,我可能有类似的东西

最旧 0 0 1 1 0 最新

如果下一个样本是1,这会变成

最旧 0 1 1 0 1 最新

如果下一个是0,这会变成...

最旧的 1 1 0 1 0 最新的

那么在简单性和内存方面实现这一点的最佳方法是什么?

我的一些想法:

Vector of bool (this would require shifting elements so seems expensive) Storing it as bits...and using bit shifting (memorywise --cheap? but is there a limit on the number of samples?) Linked lists? (might be an overkill for the task)

感谢您的想法和建议:)

最佳答案

您需要一组位。也许你可以看看 std::bitset

http://www.sgi.com/tech/stl/bitset.html

使用起来非常简单,优化的内存消耗和可能最好的性能

唯一的限制是您需要在编译时知道 n 的值。如果你想在运行时设置它,看看 boost http://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html

关于c++ - 跟踪 boolean 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5637380/

相关文章:

c++ - 返回地址上的 WriteProcessMemory

c++ - C++中打印 double 的最大宽度

c# - List 后面没有 <T> 怎么可能存在?

c++ - 将 reinterpret_cast 与 bool 一起使用

c++ - STL模板的特化

C++ QPushButton 信号和槽

mysql - MySQL GROUP BY 语句中 List 聚合函数的替代方法

java - 如何从列表中获取第一个元素 - 初学者

java - 在其他语言中,我可以对整数进行 boolean 测试。在Java中,我可以说像:这样的东西吗?

c# - 按位或 : C# versus C++