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

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

问题描述

我想在C ++中创建一个bitset。我做了一点研究。我发现的所有例子都在这里:

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

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

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>();

有任何建议吗?

帮助将非常感激。

Any suggestions?
Help would be very appreciated.

推荐答案

Boost有一个

Boost has a dynamic_bitset you can use.

或者,您也可以使用<$ c $ c> vector< bool> ,它(不幸的是)专门用作bitset。这导致了很多的混乱,一般被认为是一个坏主意。但是这是它的工作原理,所以如果这是你需要的,你可以使用它,我想。

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.

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

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