 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bruno CAUSSE Guest
|
Posted: Tue Aug 02, 2005 4:10 pm Post subject: Echanger deux tableaux |
|
|
Bonsoir,
comment transformer ce code en c++
En java
int[] tab_1 = new int[10];
int[] tab_2 = new int[10];
//swap
int[] temp = tab_1;
tab_1 = tab_2;
tab_2 = temp;
En fait j'echange 2 pointeurs (ce qui m'echange "virtuellement" mes deux
tableaux).
Avec deux tableaux en allocation auto (pile)
Un tableau auto (pile) avec un tableau (tas)
Deux tableaux tas (ce cas la est trivial, il me semble)
Merci
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Tue Aug 02, 2005 4:33 pm Post subject: Re: Echanger deux tableaux |
|
|
Bruno CAUSSE wrote:
| Quote: | Avec deux tableaux en allocation auto (pile)
Un tableau auto (pile) avec un tableau (tas)
|
std::swap_ranges(tab_1, tab_1 + N, tab_2);
à condition que les deux tableaux aient le même nombre
d'éléments N. Par contre, la complexité est de O(N).
| Quote: | Deux tableaux tas (ce cas la est trivial, il me semble)
|
Effectivement, si tab_1 et tab_2 sont des pointeurs, il suffit
d'un
std::swap(tab_1, tab_2);
avec une complexité O(1).
En revanche, en C++, on utilise les tableaux "intégrés" uniquement en
cas de nécessité absolue et on se sert de std::vector si possible.
La bonne novelle est que
std::vector<int> tab_1, tab_2;
...
std::swap(tab_1, tab_2);
fonctionne aussi avec une complexité O(1).
Falk
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|