C++ 初始化 bool 的内联向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25054365/
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
Initialize inline vector of bool
提问by www
I am using this example to initialize the bool vector:
我正在使用这个例子来初始化 bool 向量:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
map<int, vector<bool> > myMap;
vector<bool> one {true, true, false};
myMap[2] = one;
cout << myMap[2][0] << endl;
cout << myMap[2][1] << endl;
cout << myMap[2][2] << endl;
return 0;
}
The only change I made in this code is using std::vector
instead of vector and I now have:
我在这段代码中所做的唯一更改是使用std::vector
而不是向量,我现在有:
std::map<int, std::vector<bool> > m_links;
std::vector<bool> m_allFalse {false, false, false, false, false};
It tells me to use ;
after m_allFalse
. How can I get rid of this error?
它告诉我;
在m_allFalse
. 我怎样才能摆脱这个错误?
I am using intel compiler 14, but without c++11.
我正在使用英特尔编译器 14,但没有使用 c++11。
回答by Luca Davanzo
Problem is:
问题是:
std::vector<bool> m_allFalse {false, false, false, false, false};
wrong syntax in standard C++. (maybe in C++11, I don't know)
标准 C++ 中的错误语法。(也许在 C++11 中,我不知道)
You can use this instance:
你可以使用这个实例:
std::vector<bool> m_allFalse(5, false); (*)
If you want C++11 edit your tag and follow @lakesh tip.
如果你想要 C++11 编辑你的标签并遵循@lakesh 提示。
(*) this constructor is explained in vector documentation:
(*) 该构造函数在矢量文档中进行了解释:
(2) fill constructor Constructs a container with n elements. Each element is a copy of val.
(2)fill构造函数构造一个包含n个元素的容器。每个元素都是 val 的副本。
To initialize general boolean values at the beginning, you can use this way:
要在开始时初始化通用布尔值,可以使用这种方式:
bool tempBool[] = { true, false, false, true };
std::vector<bool> variousBool ( tempBool, tempBool + sizeof(tempBool) / sizeof(bool) );
Knowing this, you could create your own vector class, simply inheriting from vector (if you want you can use template to extend to all types):
知道了这一点,您可以创建自己的矢量类,只需从矢量继承即可(如果您愿意,可以使用模板扩展到所有类型):
class PimpedVector : public std::vector<bool> {
public:
PimpedVector(const unsigned int& size, ...) {
va_list args;
va_start(args, size);
for ( size_t i = 0; i < size; ++i ) {
bool b = va_arg(args, bool);
this->push_back(b);
}
}
}
So from your main you can create a PimpedVector in this way:
因此,您可以通过这种方式从主文件创建一个 PimpedVector:
PimpedVector p0(5, true, false, false, true, false);
回答by celtschk
The feature you're using was introduced in C++11. Since, as your question edit reveals, you're compiling as C++98, the compiler rightfully complains about it, because it is not valid C++98 syntax.
您正在使用的功能是在 C++11 中引入的。因为,正如您的问题编辑所揭示的,您编译为 C++98,编译器理所当然地抱怨它,因为它不是有效的 C++98 语法。
For the special case of all values being the same, the C++98 way (still working in C++11, and for this special case preferred there, too) to initialize is, as mentioned by Velthune,
对于所有值都相同的特殊情况,正如 Velthune 所提到的,C++98 方式(仍然在 C++11 中工作,并且对于这种特殊情况也首选)进行初始化,
std::vector<bool> m_allFalse(5, false);
Actually since std::vector
default-initializes all its values, and false
is the default value of bool
, for that specific case you can even simplify it to
实际上,由于std::vector
default-initializes 所有值,并且false
是 的默认值bool
,对于这种特定情况,您甚至可以将其简化为
std::vector<bool> m_allFalse(5);
If you want more general values, you'll have to copy them in, for example:
如果您想要更一般的值,则必须将它们复制进来,例如:
std::vector<bool> foo(3);
foo[0] = foo[1] = true;
foo[2] = false; // that line is actually not needed because of default initialization of members
or
或者
std::vector<bool> foo;
foo.push_back(true);
foo.push_back(true);
foo.push_back(false); // this time, it is needed because it actually created the thirs argument
An additional note: I notice the m_
prefix of your variable name; this suggests that you're declaring a membervariable (if so, that's a crucial detail which you left out). A member variable cannot (in C++98) be given an initializer (of any sort) at its declaration (with the exception of static const members of integer type). For non-static member variables, you have to initialize them at the constructor call, for example:
附加说明:我注意到m_
您的变量名的前缀;这表明您正在声明一个成员变量(如果是这样,这是您遗漏的关键细节)。成员变量不能(在 C++98 中)在其声明时被赋予一个初始化器(任何类型的)(整数类型的静态常量成员除外)。对于非静态成员变量,您必须在构造函数调用时对其进行初始化,例如:
class X
{
public:
X(); // the constructor
private:
std::vector<bool> m_Foo;
};
// constructor definition follows
X::X():
m_Foo(5, false) // this initializes your member variable `m_Foo`
{
}