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++?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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++?
Name |
---|
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.