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++?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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++?
Название |
---|
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
You linked in an interesting way (rev. 4). I didn't know C++ Reference is a CF blog post...
Btw, is vector::swap called automatically when I use swap(a,b), or I must always write a.swap(b)? I'm strongly sure it can be done with templates, but I wonder is it done.
Yes, it is done automatically: http://en.cppreference.com/w/cpp/container/vector/swap2
You do it the same way:
int (*)[100]
is a pointer to an array of 100 ints. It may be easier to define astruct
containing the array, and use pointers to such objects.Thanks! But why do you need the paranthesis in (*A)[100]=AA?
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.