何时使用STL bitsets而不是单独的变量? [英] When to use STL bitsets instead of separate variables?

查看:154
本文介绍了何时使用STL bitsets而不是单独的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下,我更适合使用bitset(STL容器)来管理一组标志,而不是将它们声明为多个单独的(bool)变量?

In what situation would it be more appropriate for me to use a bitset (STL container) to manage a set of flags rather than having them declared as a number of separate (bool) variables?

如果我使用一个bitset为50个标志而不是使用50个单独的bool变量,我会获得显着的性能提升吗?

Will I get a significant performance gain if I used a bitset for 50 flags rather than using 50 separate bool variables?

推荐答案

好,50个bool作为bitset将需要7个字节,而50个bool作为bools将需要50个字节。这些日子不是真的很大,所以使用bools可能很好。

Well, 50 bools as a bitset will take 7 bytes, while 50 bools as bools will take 50 bytes. These days that's not really a big deal, so using bools is probably fine.

但是,一个bitset可能是有用的是,如果你需要传递那些bools周围很多,特别是如果你需要从一个函数返回集合。使用bitset,你有更少的数据必须在堆栈上移动返回。然后再次,你可以使用refs,而且有更少的数据传递。 :)

However, one place a bitset might be useful is if you need to pass those bools around a lot, especially if you need to return the set from a function. Using a bitset you have less data that has to be moved around on the stack for returns. Then again, you could just use refs instead and have even less data to pass around. :)

这篇关于何时使用STL bitsets而不是单独的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆