Kako pretvoriti nize v slogu C v std::string in obratno?
Kaj so Strune v slogu C ? Ti nizi so nizi znakov, ki se končajo z znakom NULL. Nize v slogu C je mogoče deklarirati na naslednje načine:
Deklaracija in inicializacija
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() in še veliko več! (Vse te funkcije so članske funkcije ' cstring ' glava ). Kaj je std::string? Standardna knjižnica C++ vsebuje funkcije in razrede. Niz je eden od njegovih razredov. Tukaj imamo opravka z objektom razreda nizov. Ta std::string skrbi zase in upravlja lasten pomnilnik.
Deklaracija in inicializacija
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: HELLOPretvarjanje C-niza v std::string. Toda zakaj potrebujemo to preobrazbo? Iz niza C v std::string? To je zato, ker
- Std::string upravlja svoj prostor. Programerju torej ni treba skrbeti za pomnilnik za razliko od nizov C (ker so niz znakov)
- Z njimi je enostavno upravljati. Operator '+' za veriženje '=' za dodelitev je mogoče primerjati z običajnimi operatorji.
- Iteratorje je mogoče uporabiti v std::string in ne v nizih C. And many more! Here is the code for it:- CPP
- Ker je v glavi več zmogljivih funkcij, nam delo zelo olajša.
/* 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);Pretvorba std::string v niz v slogu C Zakaj potrebujemo to preobrazbo? Iz std::string v niz C?
/* 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. Sorodni članki: Razred nizov C++ in njegove aplikacije | Komplet 1 Razred nizov C++ in njegove aplikacije | Komplet 2