我需要跟踪 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/