Reputation: 31
I'm trying to add 100 elements in a vector located inside a class, but I can't use a for loop because the vector is defined outside of a function. Is there any way for me to add the values without having to manually list them?
class Player
{
vector<bool> able; // set 10 values to 0
vector<bool> tertiary; // set 100 values to 0
void other_funcs()
{
//stuff
}
}
Upvotes: 2
Views: 422
Reputation: 48605
Values of bool
should really be true
or false
although 0
will convert to false
.
If you want to set your members as indicated in your code you can do that this way:
class Player
{
std::vector<bool> able = std::vector<bool>(10, false);
std::vector<bool> tertiary = std::vector<bool>(100, false);
void other_funcs()
{
}
};
The statement std::vector<bool>(10, false)
constructs a vector with 10
elements all set to false
.
Upvotes: 1
Reputation: 410
Use std::fill
:
std::fill(able.begin(), able.begin() + 10, 0);
Make sure your vector has enough space to do so:
std::vector<bool> able(10);
Upvotes: 2