PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > swap char array.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
swap char array.

Réponse
 
LinkBack Outils de la discussion
Vieux 26/12/2007, 18h33   #1
NK
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut swap char array.

char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;


}



but to swap


char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))



Thanx in advance
  Réponse avec citation
Vieux 26/12/2007, 21h24   #2
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: swap char array.

NK wrote:
> char *a="nnnnnnn fgjfjgjf";
> char *b="ABC jjjjjjjjjhhhh";
>
> to swap these two string......
> this swap fn is working
>
> template<class T>
> void Swap( const T *&a,const T *&b)//no need to write const
> {
> T *temp;
> temp=a;
>
> a=b;
>
> b=temp;
>
>
> }
>
>
>
> but to swap
>
>
> char a[]="nhfhdhfhh fgjfjgjf";
> char b[]="ABC jjjjjjjjjhhhh";
>
> these char array its not.
> why?
> how a[] and b[] can be swapped?
> give effficient soln.
> don't use swap fn of string library(string1.swap(string2))
>
>


Here you go:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
  Réponse avec citation
Vieux 27/12/2007, 08h21   #3
Rahul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: swap char array.

On Dec 26, 11:33 pm, NK <nagendra...@gmail.com> wrote:
> char *a="nnnnnnn fgjfjgjf";
> char *b="ABC jjjjjjjjjhhhh";
>
> to swap these two string......
> this swap fn is working
>
> template<class T>
> void Swap( const T *&a,const T *&b)//no need to write const
> {
> T *temp;
> temp=a;
>
> a=b;
>
> b=temp;
>
> }
>
> but to swap
>
> char a[]="nhfhdhfhh fgjfjgjf";
> char b[]="ABC jjjjjjjjjhhhh";
>
> these char array its not.
> why?
> how a[] and b[] can be swapped?
> give effficient soln.
> don't use swap fn of string library(string1.swap(string2))
>
> Thanx in advance


swap function could also check if both the reference are pointing to
same variable or not...
  Réponse avec citation
Vieux 27/12/2007, 09h53   #4
tragomaskhalos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: swap char array.

On 26 Dec, 18:33, NK <nagendra...@gmail.com> wrote:
> char *a="nnnnnnn fgjfjgjf";
> char *b="ABC jjjjjjjjjhhhh";
>
> to swap these two string......
> this swap fn is working
>
> template<class T>
> void Swap( const T *&a,const T *&b)//no need to write const
> {
> T *temp;
> temp=a;
>
> a=b;
>
> b=temp;
>
> }
>
> but to swap
>
> char a[]="nhfhdhfhh fgjfjgjf";
> char b[]="ABC jjjjjjjjjhhhh";
>
> these char array its not.
> why?
> how a[] and b[] can be swapped?
> give effficient soln.
> don't use swap fn of string library(string1.swap(string2))
>
> Thanx in advance


Obviously homework, but it's Christmas ...

You can't do this directly - arrays are not assignable.
You could do a character-by-character swap, but then
what if the arrays are of different lengths?

But all of this is academic, you can achieve what you
need like this:

char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

char* ap = a;
char* bp = b;

// use ap for a, bp for b

std::swap(ap, bp);

// ap now points to b[], bp to a[]


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11637 seconds with 12 queries