Hoe converteer ik strings in C-stijl naar std::string en vice versa?
Wat zijn Snaren in C-stijl ? Deze tekenreeksen bestaan uit een reeks tekens die eindigen met een NULL-teken. Strings in C-stijl kunnen op de volgende manieren worden gedeclareerd:
Verklaring en initialisatie
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-3C style strings are operated with very useful functions like strcpy() strlen() strpbrk() Strach() strstr() en nog veel meer! (Al deze functies zijn lidfuncties van ' cstring ' kop ). Wat is een std::string? De standaardbibliotheek van C++ bevat functies en klassen. String is een van de klassen. Hier hebben we te maken met een object van stringklasse. Deze std::string zorgt voor zichzelf en beheert zijn eigen geheugen.
Verklaring en initialisatie
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: HELLOC-String converteren naar een std::string. Maar waarom hebben we deze transformatie nodig? Van een C-snaar naar een std::string? Het is omdat
- Std::string beheert zijn eigen ruimte. Programmeurs hoeven zich dus geen zorgen te maken over het geheugen, in tegenstelling tot C-strings (aangezien het een reeks tekens is)
- Ze zijn eenvoudig te bedienen. ‘+’ operator voor aaneenschakeling '=' voor toewijzing kan worden vergeleken met reguliere operators.
- Iterators kunnen worden gebruikt in std::string en niet in C-strings. And many more! Here is the code for it:- CPP
- Het is omdat er verschillende krachtige functies in header zitten die ons werk een stuk eenvoudiger maken.
/* 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 : TestingThe above conversion also works for character array.
// Character array to std::string conversion char a[] = 'Testing'; string s(a);Een std::string converteren naar een string in C-stijl Waarom hebben we deze transformatie nodig? Van std::string naar een C-string?
/* 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 : Testingstd::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 : TestingBoth C strings and std::strings have their own advantages. One should know conversion between them to solve problems easily and effectively. Gerelateerde artikelen: C++ stringklasse en zijn toepassingen | Set 1 C++ stringklasse en zijn toepassingen | Stel 2 in
Dit Vind Je Misschien Leuk
Top Artikelen
Categorie
Interessante Artikelen