Hvordan konverterer man C stil strenge til std::streng og omvendt?
Hvad er C stil strenge ? Disse strenge er en række af tegn, der afsluttes med et NULL-tegn. C-stilstrenge kan erklæres på følgende måder:
Erklæring og initialisering
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() og mange flere!(Alle disse funktioner er medlemsfunktioner af ' cstring ' overskrift). Hvad er en std::streng? C++ standardbibliotek indeholder funktioner og klasser. String er en af dens klasser. Her beskæftiger vi os med et objekt af strengklasse. Denne std::streng passer sig selv og styrer sin egen hukommelse.
Erklæring og initialisering
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: HELLOKonvertering af C-streng til en std::streng. Men hvorfor har vi brug for denne transformation? Fra en C-streng til en std::streng? Det er fordi
- Std::string styrer sit eget rum. Så programmør behøver ikke at bekymre sig om hukommelse i modsætning til C-strenge (da de er en række tegn)
- De er nemme at betjene. '+'-operator for sammenkædning '=' for tildeling kan sammenlignes med almindelige operatorer.
- Iteratorer kan bruges i std::streng og ikke i C-strenge. And many more! Here is the code for it:- CPP
- Det er fordi der er flere kraftfulde funktioner i header, der gør vores arbejde meget nemmere.
/* 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);Konvertering af en std::streng til en C-stilstreng Hvorfor har vi brug for denne transformation? Fra std::streng til en C streng?
/* 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. Relaterede artikler: C++ strengklasse og dens applikationer | Sæt 1 C++ strengklasse og dens applikationer | Sæt 2
Du Kan Måske Lide
Top Artikler
Kategori
Interessante Artikler