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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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.