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.
我将使用此方法,该方法将重用您在问题中声明的x
变量。
std::fill(hello.begin(), hello.end(), x);
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);
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.