straw__hat's blog

By straw__hat, history, 6 years ago, In English

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

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it -18 Vote: I do not like it
for (int i = 0; i < 200; i++) {
    p[i].clear();
}
  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have just tested this, but it said that pair has no member named clear, so where is the problem please.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it
memset(p,0,sizeof p);
»
6 years ago, # |
  Vote: I like it +26 Vote: I do not like it
	for (auto &i : p) i = {0, {0, 0}};
»
6 years ago, # |
  Vote: I like it +39 Vote: I do not like it
fill(p, p + 200, {0, {0, 0}});
»
6 years ago, # |
Rev. 2   Vote: I like it -11 Vote: I do not like it

for vector...!!!

for (int i = 0; i < 200; i++) { v[i].clear(); }

Or

fill(v, v + 200, {0, {0, 0}});

Which one is better ??? i usually use the first one.....

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    How can you usually use the first one, if it doesn't even work? xd

»
6 years ago, # |
  Vote: I like it +8 Vote: I do not like it

in order to avoid running a loop, you can make it like this :-

vector< pair<ll , pair<ll,ll> > > p; and then resize as u want it.

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      yes you just have to be careful with that . .

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      aboAdnan He mentioned resizing the vector, which default initializes the vector. It is going to work perfectly fine what the OP asked for.

      • »
        »
        »
        »
        6 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        I thought the question is about resetting the array (assigning zero to each element of it), but resize doesn't do that, does it?

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          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.

»
6 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

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.