Initialize inline vector of bool
Question
I am using this example to initialize the bool vector:
#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::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?
I am using intel compiler 14, but without c++11.
Solution
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)
You can use this instance:
std::vector<bool> m_allFalse(5, false); (*)
If you want C++11 edit your tag and follow @lakesh tip.
(*) this constructor is explained in vector documentation:
(2) fill constructor Constructs a container with n elements. Each element is a copy of 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 p0(5, true, false, false, true, false);
OTHER TIPS
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.
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,
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<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 member variable (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:
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`
{
}