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 | 3773 |
3 | Radewoosh | 3646 |
4 | ecnerwala | 3624 |
5 | jqdai0815 | 3620 |
5 | Benq | 3620 |
7 | orzdevinwang | 3612 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | cry | 161 |
2 | maomao90 | 160 |
3 | awoo | 159 |
4 | atcoder_official | 157 |
5 | -is-this-fft- | 155 |
5 | nor | 155 |
5 | adamant | 155 |
8 | Um_nik | 152 |
8 | maroonrk | 152 |
10 | djm03178 | 146 |
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.