C スタイルの文字列を std::string に、またはその逆に変換するにはどうすればよいですか?
とは何ですか C スタイルの文字列 ? これらの文字列は、NULL 文字で終わる文字の配列です。 C スタイルの文字列は次の方法で宣言できます。
宣言と初期化
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() ストラッハ() strstr() などなど!(これらの関数はすべて ' のメンバー関数です) cstring ' ヘッダー)。 std::string とは何ですか? C++ 標準ライブラリには関数とクラスが含まれています。 String はそのクラスの 1 つです。ここでは文字列クラスのオブジェクトを扱います。この std::string はそれ自体を処理し、独自のメモリを管理します。
宣言と初期化
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 を std::string に変換します。 しかし、なぜこのような変革が必要なのでしょうか? C 文字列から std::string へ?それは、
- Std::string は独自のスペースを管理します。そのため、プログラマは C 文字列とは異なりメモリについて心配する必要がありません (文字列は文字の配列であるため)。
- 操作は簡単です。連結の「+」演算子、代入の「=」は、通常の演算子を使用して比較できます。
- イテレータは std::string で使用できますが、C 文字列では使用できません。 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 : TestingThe above conversion also works for character array.
// Character array to std::string conversion char a[] = 'Testing'; string s(a);std::string を C スタイルの文字列に変換する なぜこのような変革が必要なのでしょうか? std::string から 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. 関連記事: C++ 文字列クラスとそのアプリケーション |セット1 C++ 文字列クラスとそのアプリケーション |セット2