### straw__hat's blog

By straw__hat, history, 8 months ago, ,

I want to reuse it . How am I supposed to do it?

•
• +3
•

 » 8 months ago, # |   -18 for (int i = 0; i < 200; i++) { p[i].clear(); } 
•  » » 8 months ago, # ^ |   0 I have just tested this, but it said that pair has no member named clear, so where is the problem please.
 » 8 months ago, # |   0 memset(p,0,sizeof p); 
 » 8 months ago, # |   +26  for (auto &i : p) i = {0, {0, 0}}; 
 » 8 months ago, # |   +39 fill(p, p + 200, {0, {0, 0}}); 
 » 8 months ago, # | ← Rev. 2 →   -11 for vector...!!!for (int i = 0; i < 200; i++) { v[i].clear(); }Orfill(v, v + 200, {0, {0, 0}});Which one is better ??? i usually use the first one.....
•  » » 8 months ago, # ^ |   +4 How can you usually use the first one, if it doesn't even work? xd
•  » » » 8 months ago, # ^ |   0 thought it was a vector .... sry
 » 8 months ago, # |   +8 in order to avoid running a loop, you can make it like this :-vector< pair > > p; and then resize as u want it.
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 Yes, but p.clear() will not assign 0's to the vector's indexes, it will just make the pointer of this vector points to the first index again so if you use push_back method for example, it will push in the first index.But if you call p[x] just after clearing the vector, it will give you what index 'x' had before clearing.
•  » » » 8 months ago, # ^ |   +1 yes you just have to be careful with that . .
•  » » » 8 months ago, # ^ |   +1 MahmoudAlio He mentioned resizing the vector, which default initializes the vector. It is going to work perfectly fine what the OP asked for.
•  » » » » 8 months ago, # ^ | ← Rev. 2 →   0 I thought the question is about resetting the array (assigning zero to each element of it), but resize doesn't do that, does it?
•  » » » » » 8 months ago, # ^ |   +1 Yeah. If you provide a value with resize, they will fill the value with it. Otherwise, they will be filled with default value, zero in this case.He mentioned you want to eliminate the loop with that. But, this wont reduce the runtime.
 » 8 months ago, # | ← Rev. 2 →   +5 Defining clear would be better. There is nothing like clearing an array. Maybe, You meant zero initialize. Some people uses memset which is not going to be portable. I prefer using fill function.
•  » » 8 months ago, # ^ |   0 So is that the case that pair< pair > p[200] can never be cleared like we do in vectors?