Изменения рейтингов за последние раунды временно удалены. Скоро они будут возвращены. ×

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

Автор PeterNaut, 10 лет назад, По-английски

In order to swap two arrays in O(1) you can do this using pointers:

#include <algorithm>
int AA[100], *A=AA, BB[100], *B=BB;
swap(A, B);

But how can you do that for matrixes in C++?

Теги c++
  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

»
10 лет назад, # |
Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

First, you do not need to make *A and *B. swap (AA,BB) will do.

Second, matrices are just arrays too so they can be swapped too in constant time. (even if array elements are dynamically created using new keyword).

Lastly, if you still do not believe something; vectors make it more convenient

»
10 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You do it the same way:

int AA[100][100], (*A)[100]=AA, BB[100][100], (*B)[100]=BB;    
swap(A, B);

int (*)[100] is a pointer to an array of 100 ints. It may be easier to define a struct containing the array, and use pointers to such objects.

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

    Thanks! But why do you need the paranthesis in (*A)[100]=AA?

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

      int (*A)[100] is a pointer to an array of 100 ints. int *A[100] is an array of 100 pointers to ints. You can google for "c declaratons" for more reading.