简体   繁体   English

的std ::矢量 <bool> a(b,错误); 在C#中

[英]std::vector<bool> a (b, false); in c#

Can someone please help me understand what the following code means and how it translates to C#? 有人可以帮我理解以下代码的含义以及如何将其转换为C#吗?

unsigned int b = 100;
std::vector<bool> a (b, false);

if it was something like this: 如果是这样的话:

vector<bool> a;

I'd probably just go: 我可能会去:

List<bool> a;

Is that correct? 那是对的吗?

But I don't don't know c++, so I don't understand how a uint and a bool are being passed to a vector of type bool? 但是我不懂C ++,所以我不明白如何将uint和bool传递给bool类型的向量? Maybe it should be something like this? 也许应该是这样的吗?

MyGenericList<int, bool> a = new MyGenericList<int, bool>(b, false);

std::vector<bool> is special, it is implemented as a bit array in C++. std::vector<bool>是特殊的,它在C ++中作为位数组实现。 In other words, 1 byte stores 8 elements of the vector, the most condense possible way to create an array of bool. 换句话说,1个字节存储向量的8个元素,这是创建布尔数组的最紧凑的可能方式。 That's available in .NET as well, the exact equivalent declaration is: 在.NET中也可以使用,确切的等效声明是:

    int b = 100;
    System.Collections.BitArray a = new System.Collections.BitArray(b);

If this array only ever contains 100 elements then, meh, don't bother. 如果此数组仅包含100个元素,那么,不要打扰。 If it is going to contain a million then, yes, do bother. 如果将要包含一百万,那么,是的,麻烦了。

As the other answers say, the C++ code creates something like an array or list with 100 elements, all false . 就像其他答案所说的那样,C ++代码创建的东西类似于带有100个元素的数组或列表,全为false The equivalent in C# would be: C#中的等效项为:

var a = new bool[100];

Or if you don't need a fixed size: 或者,如果您不需要固定大小:

var a = new List<bool>(100);
// or, if the initial capacity isn't important
var a = new List<bool>();

Since the default bool value is false , you don't need to explicitly specify it anywhere. 由于默认的bool值为false ,因此您无需在任何地方显式指定它。 If you wanted true (or, eg in a list of int s, to default to -1 ), you'd have to loop through it after you created it: 如果您想要true (或者,例如,在int列表中,默认为-1 ),则在创建它之后必须遍历它:

var a = new bool[100];
for (var i = 0; i < a.Length; i++)
    a[i] = true;

std::vector<bool> a (b, false); creates a vector (an array or kind of a C# list) that is 100 bools long and initializes these bool s to false. 创建一个长度为100 bool的向量(数组或C#列表的一种),并将这些bool初始化为false。

A note: Do not use std::vector<bool> . 注意事项: 不要使用std::vector<bool>

The first argument is the size of vector while second is it's element (see handly C++ reference ). 第一个参数是向量的大小,第二个参数是向量的元素(请参阅C ++参考手册 )。 So 所以

std::vector<bool> a(100, false);

Create vector of 100 elements, elements of which are false . 创建100个元素的向量,其中元素为false

As pointed out by Scarlet std::vector<bool> is 'strange' (it does not have full C++ container interface. I'd not go so far to say 'don't use it' but it's important to know that it might behave strange - the ling above is to 'normal' vector). 正如Scarlet std::vector<bool>所指出的那样,它是“奇怪的”(它没有完整的C ++容器接口。我不会走得太远,不能说“不要使用它”,但重要的是要知道它可能行为很奇怪-上面的代码是“正常”向量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM