在初始化时定义位大小? [英] Define bitset size at initialization?

查看:75
本文介绍了在初始化时定义位大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中做一个位。我做了一些研究。我发现的所有示例都是这样的:

I want to make a bitset in C++. I did a bit of research. All examples I found where like this:

bitset<6> myBitset;
// do something with it

但是我不知道位的大小当我在类中定义变量时:

But I don't know the size of the bitset when I define the variable in my class:

#include <bitset>
class Test
{
public:
     std::bitset *myBitset;
}

这不会编译...

像这样的初始化也不起作用:

And initializing like this also doesn't work:

int size = getDependentSizeForBitset();
myBitset = new bitset<size>();


推荐答案

Boost具有 dynamic_bitset 即可使用。

Boost has a dynamic_bitset you can use.

或者,您可以使用 vector< bool> ,(不幸的是)它专门用作位集。这引起了很多混乱,通常被认为是一个坏主意。但这就是它的工作方式,所以我想,如果您需要它,那么不妨使用它。

Alternatively, you can use a vector<bool>, which (unfortunately) is specialized to act as a bitset. This causes a lot of confusion, and in general is considered a bad idea. But that's how it works, so if that's what you need, you might as well use it, I suppose.

这篇关于在初始化时定义位大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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