Блог пользователя straw__hat

Автор straw__hat, история, 6 лет назад, По-английски

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

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится -18 Проголосовать: не нравится
for (int i = 0; i < 200; i++) {
    p[i].clear();
}
  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
memset(p,0,sizeof p);
»
6 лет назад, # |
  Проголосовать: нравится +26 Проголосовать: не нравится
	for (auto &i : p) i = {0, {0, 0}};
»
6 лет назад, # |
  Проголосовать: нравится +39 Проголосовать: не нравится
fill(p, p + 200, {0, {0, 0}});
»
6 лет назад, # |
Rev. 2   Проголосовать: нравится -11 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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 лет назад, # ^ |
    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.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      yes you just have to be careful with that . .

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

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

      • »
        »
        »
        »
        6 лет назад, # ^ |
        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?

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится +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.

»
6 лет назад, # |
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.