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 | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 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++?
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.