Kako pretvoriti nizove C stila u std::string i obrnuto?

Što su Žice u stilu C ? Ovi nizovi su niz znakova koji završavaju znakom NULL. Nizovi u stilu C mogu se deklarirati na sljedeće načine:

Deklaracija i inicijalizacija

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 strcpy() strlen() strpbrk() strach() strstr() i još mnogo toga! (Sve ove funkcije su funkcije članice ' cstring ' zaglavlje ). Što je std::string? C++ standardna biblioteka sadrži funkcije i klase. String je jedna od njegovih klasa. Ovdje imamo posla s objektom klase string. Ovaj std::string brine sam o sebi i upravlja vlastitom memorijom.

Deklaracija i inicijalizacija

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  
Pretvaranje C-stringa u std::string. Ali zašto nam je potrebna ova transformacija? Od C niza do std::stringa? To je zato što
  • Std::string upravlja vlastitim prostorom. Dakle, programer ne treba brinuti o memoriji za razliku od C nizova (budući da su niz znakova)
  • Jednostavni su za rukovanje. '+' operator za ulančavanje '=' za dodjelu može se usporediti korištenjem uobičajenih operatora.
  • string::find() i mnoge druge funkcije mogu se implementirati na std::string, a ne na C-Strings tako da ovo postaje zgodno.
  • Iteratori se mogu koristiti u std::string, a ne u C-stringovima.
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);  
Pretvaranje std::stringa u niz C stila Zašto nam je potrebna ova transformacija? Od std::string do C niza?
  • To je zato što postoji nekoliko moćnih funkcija u zaglavlju koje nam uvelike olakšavaju rad.
  • vući() gušiti() i mnoge druge funkcije rade samo s C nizovima.
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  (  '%s  n  '       a  );         return     0  ;   }   
Output:
This is a std::string : Testing This is a C-String : Testing  
std::string also has a function c_str() 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  (  '%s  n  '       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. Povezani članci: Klasa nizova C++ i njezine primjene | Set 1 Klasa nizova C++ i njezine primjene | Set 2