user3150601
user3150601

Reputation: 1

Fastest way to zero out a vector<vector<bool> >

I've tried something like this:

vector<bool> x(10, 0);
vector<vector<bool> > hello(10, x);

/*some modifications on hello*/

memset(&hello, 0, sizeof(hello));

And my program compiles, but it breaks. Any idea how I can do this operation as quickly as possible? I know that the memset probably isn't working because of the nested vector, but I'm not sure how to accomplish this task.

Upvotes: 0

Views: 358

Answers (3)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145359

With the code you have, I'd write …

for( auto& v : hello ) { v = x; }

assuming x has remained as all-zeros. It's clear enough and avoids dragging in <algorithm>.

Hewever, it will probably be faster to change the representation of your bit matrix, from a vector of vectors to a single vector, or if it's fixed size, to a single bitset.

Upvotes: 0

111111
111111

Reputation: 16168

for(auto& bv : hello) 
    std::fill(begin(bv), end(bv), false);

Or if you want to use x as the prototype

std::fill(begin(hello), end(hello), x);

Upvotes: 1

Sam Harwell
Sam Harwell

Reputation: 99949

I would use this, which reuses the x variable you declared in the question.

std::fill(hello.begin(), hello.end(), x);

Upvotes: 1

Related Questions