logo

Come convertire le stringhe in stile C in std::string e viceversa?

Cosa sono ? Queste stringhe sono matrici di caratteri che terminano con un carattere NULL. Le stringhe in stile C possono essere dichiarate nei seguenti modi:

Dichiarazione e inizializzazione

CPP
/* To demonstrate C style strings */ #include   using namespace std; int main() {  /* Null character has to be added explicitly */  char str1[8] = {'H'  'E'  'L'  'L'  'O'   '-''1''' };    /* Compiler implicitly adds Null character */  char str2[] = 'HELLO-2' ;     /* Compiler implicitly adds Null character.   Note that string literals are typically stored  as read only */  const char *str3 = 'HELLO-3' ;  cout << str1 << endl << str2 << endl << str3;  return 0; }  
Output:
HELLO-1 HELLO-2 HELLO-3 
C style strings are operated with very useful functions like strlen() straccio() e molte altre!(Tutte queste funzioni sono funzioni membro di ' La libreria standard C++ contiene funzioni e classi. String è una delle sue classi. Qui abbiamo a che fare con un oggetto della classe string. Questo std::string si prende cura di se stesso e gestisce la propria memoria.

Dichiarazione e inizializzazione



CPP
/* To demonstrate std::string */ #include   #include  using namespace std; int main() {  /* s becomes object of class string. */  string s;   /* Initializing with a value. */   s = 'HELLO';  /* Printing the value */   cout << s;     return 0; } 
Output:
HELLO 
Conversione di C-String in std::string. Ma perché abbiamo bisogno di questa trasformazione? Da una stringa C a una std::string? È perché
  • Std::string gestisce il proprio spazio. Quindi il programmatore non deve preoccuparsi della memoria a differenza delle stringhe C (poiché sono una serie di caratteri)
  • Sono facili da usare. L'operatore '+' per la concatenazione '=' per l'assegnazione può essere confrontato utilizzando gli operatori regolari.
  • e molte altre funzioni possono essere implementate su std::string e non su C-String, quindi questo diventa utile.
  • Gli iteratori possono essere utilizzati in std::string e non in C-string.
And many more! Here is the code for it:- CPP
/* To demonstrate C style string to std::string */ #include   using namespace std; int main() {  /*Initializing a C-String */  const char *a = 'Testing';   cout << 'This is a C-String : '<< a << endl;  /* This is how std::string s is assigned  though a C string ‘a’ */  string s(a);     /* Now s is a std::string and a is a C-String */  cout << 'This is a std::string : '<< s << endl;  return 0; } 
Output:
This is a C-String : Testing This is a std::string : Testing 
The above conversion also works for character array.
 // Character array to std::string conversion char a[] = 'Testing'; string s(a); 
Conversione di una std::string in una stringa in stile C Perché abbiamo bisogno di questa trasformazione? Da std::string a una stringa C?
  • È perché ci sono diverse potenti funzioni nell'intestazione che rendono il nostro lavoro molto più semplice.
  • trainare() soffocamento() e molte altre funzioni funzionano solo con stringhe C.
You can think of other reasons too! Here is the code for conversion:- CPP
/* To demonstrate std::string to C style string */ #include   #include /* This header contains string class */ using namespace std; int main() {  /* std::string initialized */  string s = 'Testing';   cout << 'This is a std::string : '<< s << endl;  /* Address of first character of std::string is   stored to char pointer a */  char *a = &(s[0]);   /* Now 'a' has address of starting character  of string */  printf('%sn' a);   return 0; } 
Output:
This is a std::string : Testing This is a C-String : Testing 
std::string also has a function that can be used to get a null terminated character array. CPP
/* To demonstrate std::string to C style string using  c_str() */ #include   using namespace std; int main() {  /* std::string initialized */  string s = 'Testing';   cout << 'This is a std::string : '<< s << endl;  // c_str returns null terminated array of characters  const char *a = s.c_str();  /* Now 'a' has address of starting character  of string */  printf('%sn' a);   return 0; } 
Output:
This is a std::string : Testing This is a C-String : Testing 
Both C strings and std::strings have their own advantages. One should know conversion between them to solve problems easily and effectively. Articoli correlati: Classe stringa C++ e sue applicazioni | Set 1 Classe stringa C++ e sue applicazioni | Set 2