C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Echanger deux tableaux

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Bruno CAUSSE
Guest





PostPosted: Tue Aug 02, 2005 4:10 pm    Post subject: Echanger deux tableaux Reply with quote



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





PostPosted: Tue Aug 02, 2005 4:33 pm    Post subject: Re: Echanger deux tableaux Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.