C++ 在初始化时定义位集大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3134718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:09:00  来源:igfitidea点击:

Define bitset size at initialization?

c++bitset

提问by Martijn Courteaux

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

我想用 C++ 做一个 bitset。我做了一些研究。我发现的所有例子都是这样的:

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:

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

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

This won't compile...

这不会编译...

And initializing like this also doesn't work:

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

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

采纳答案by jalf

Boost has a dynamic_bitsetyou can use.

Boost 有一个dynamic_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.

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

回答by Thomas Jones-Low

回答by graham.reeds

You should check out boosts dynamic_bitset.

您应该查看 boosts dynamic_bitset。

回答by AnT

What you are saying at the beginning is not true. The "examples you found" did not look as you posted. It is impossible to use a non-constant value to parametrize a template. So, your first example is invalid. Only constant expressions can serve as non-type arguments for a template. I.e. the non-type argument has to be a compile-time constant.

你一开始说的不是真的。“您找到的示例”看起来不像您发布的那样。不可能使用非常量值来参数化模板。所以,你的第一个例子是无效的。只有常量表达式可以作为模板的非类型参数。即非类型参数必须是编译时常量。

Of looks like you want to create a bitset whose size is not a compile-time constant. In this case the bitsettemplate is out of question. You need an implementation of run-time sized bitset. For example, you can use std::vector<bool>- in many (if not all) implementations this template is specialized to implement a packedarray of boolean values, where each element occupies one bit (as opposed to an boolobject).

看起来您想创建一个大小不是编译时常量的位集。在这种情况下,bitset模板是不可能的。您需要一个运行时大小的位集的实现。例如,您可以使用std::vector<bool>- 在许多(如果不是全部)实现中,此模板专门用于实现布尔值的打包数组,其中每个元素占用一位(与bool对象相反)。

回答by stinky472

bitset requires size as a template parameter, meaning the size has to be capable of being determined at compile-time. It cannot be based on a runtime condition, like user input.

bitset 需要 size 作为模板参数,这意味着必须能够在编译时确定大小。它不能基于运行时条件,例如用户输入。

For that, you should look into std::vector or boost::dynamic_bitset. std::vector is a specialized template instantiation that uses one bit per element. Unlike bitset, it can be dynamically sized.

为此,您应该查看 std::vector 或 boost::dynamic_bitset。std::vector 是一个专门的模板实例化,每个元素使用一位。与 bitset 不同,它可以动态调整大小。

回答by stinky472

You can make your class a template to make the std::bitsetsize undetermined until your class gets instantiated. You can do it like this:

你可以让你的类成为一个模板,在std::bitset你的类被实例化之前不确定大小。你可以这样做:

#include <bitset>

template<int size>
class Test
{
public: 
    std::bitset<size> bitset;
    //...
}

Then to use your class you would have to do this:

然后要使用您的课程,您必须这样做:

int exampleSize = 42;
Test<exampleSize> name;