C++ の std::string クラス
C++ の定義には、 クラスのオブジェクトとしての文字のシーケンス 。このクラスは std::string と呼ばれます。 string クラスは、文字をバイトのシーケンスとして格納し、次の機能を備えています。 半角文字へのアクセス 。
文字列と文字配列
| 弦 | 文字配列 |
|---|---|
| 文字列とは、 オブジェクトを定義するクラス 文字のストリームとして表現されます。 | 文字配列は単に 文字の配列 NULL 文字で終了することができます。 |
| 文字列の場合、メモリは 動的に割り当てられる 。実行時にオンデマンドでより多くのメモリを割り当てることができます。メモリは事前に割り当てられていないため、 メモリが無駄になることはありません 。 | 文字配列のサイズは次のようにする必要があります。 静的に割り当てられる 必要に応じて、実行時にこれ以上のメモリを割り当てることはできません。未使用の割り当て済み メモリも無駄になる |
| 文字列はオブジェクトとして表現されるため、 配列の減衰なし が発生します。 | があります の脅威 配列の減衰 文字配列の場合。 |
| 文字列が遅い 文字配列よりも実装と比較した場合。 | の実装 文字配列のほうが速い std:: 文字列よりも。 |
| 文字列クラスの定義 多くの機能 文字列に対する多様体操作を可能にします。 | 文字配列 提供しない 多くの 組み込み関数 文字列を操作します。 |
文字列の操作
1) 入力機能
| 関数 | 意味 |
|---|---|
| getline() | この関数は、ユーザーが入力した文字のストリームをオブジェクト メモリに保存するために使用されます。 |
| プッシュバック() | この関数は、文字列の末尾に文字を入力するために使用されます。 |
| ポップバック() | C++11 から導入された (文字列用) この関数は、文字列から最後の文字を削除するために使用されます。 |
例:
CPP // C++ Program to demonstrate the working of // getline() push_back() and pop_back() #include #include // for string class using namespace std ; // Driver Code int main () { // Declaring string string str ; // Taking string input using getline() getline ( cin str ); // Displaying string cout < < 'The initial string is : ' ; cout < < str < < endl ; // Inserting a character str . push_back ( 's' ); // Displaying string cout < < 'The string after push_back operation is : ' ; cout < < str < < endl ; // Deleting a character str . pop_back (); // Displaying string cout < < 'The string after pop_back operation is : ' ; cout < < str < < endl ; return 0 ; }
出力
The initial string is : The string after push_back operation is : s The string after pop_back operation is :
時間計算量: O(1)
空間の複雑さ: O(n) ここで、n は文字列のサイズです
2) 容量関数
| 関数 | 意味 |
|---|---|
| 容量() | この関数は、文字列に割り当てられた容量を返します。この容量は文字列のサイズ以上である可能性があります。新しい文字が文字列に追加されるときに操作を効率的に実行できるように、追加のスペースが割り当てられます。 |
| サイズ変更() | この関数は文字列のサイズを変更し、サイズを増減できます。 |
| 長さ() | この関数は文字列の長さを調べます。 |
| シュリンクトゥフィット() | この関数は、文字列の容量を減らし、文字列の最小容量と等しくします。この操作は、文字をさらに追加する必要がないことが確実な場合に追加のメモリを節約するのに役立ちます。 |
例:
CPP // C++ Program to demonstrate the working of // capacity() resize() and shrink_to_fit() #include #include // for string class using namespace std ; // Driver Code int main () { // Initializing string string str = 'geeksforgeeks is for geeks' ; // Displaying string cout < < 'The initial string is : ' ; cout < < str < < endl ; // Resizing string using resize() str . resize ( 13 ); // Displaying string cout < < 'The string after resize operation is : ' ; cout < < str < < endl ; // Displaying capacity of string cout < < 'The capacity of string is : ' ; cout < < str . capacity () < < endl ; // Displaying length of the string cout < < 'The length of the string is :' < < str . length () < < endl ; // Decreasing the capacity of string // using shrink_to_fit() str . shrink_to_fit (); // Displaying string cout < < 'The new capacity after shrinking is : ' ; cout < < str . capacity () < < endl ; return 0 ; }
出力
The initial string is : geeksforgeeks is for geeks The string after resize operation is : geeksforgeeks The capacity of string is : 26 The length of the string is :13 The new capacity after shrinking is : 13
時間計算量: O(1)
空間の複雑さ: O(n) ここで、n は文字列のサイズです
3) イテレータ関数
| 関数 | 意味 |
|---|---|
| 始める() | この関数は、文字列の先頭に反復子を返します。 |
| 終わり() | この関数は、文字列の末尾の次の反復子を返します。 |
| rbegin() | この関数は、文字列の末尾を指す逆反復子を返します。 |
| 与える() | この関数は、文字列の先頭の前を指す逆反復子を返します。 |
| cbegin() | この関数は、文字列の先頭を指す定数反復子を返します。この関数が指す内容を変更するために使用することはできません。 |
| いくつか() | この関数は、文字列の末尾の次を指す定数反復子を返します。この関数が指す内容を変更するために使用することはできません。 |
| crbegin() | この関数は、文字列の末尾を指す定数逆反復子を返します。この関数は、それが指す内容を変更するために使用することはできません。 |
| クレンド() | この関数は、文字列の先頭より前を指す定数逆反復子を返します。この関数は、それが指す内容を変更するために使用することはできません。 |
アルゴリズム:
- 文字列を宣言する
- すべてのタイプのイテレータを使用して文字列を反復してみます。
- 文字列の要素を変更してみてください。
- すべての反復を表示します。
例:
CPP // C++ Program to demonstrate the working of // begin() end() rbegin() rend() cbegin() cend() crbegin() crend() #include #include // for string class using namespace std ; // Driver Code int main () { // Initializing string` string str = 'geeksforgeeks' ; // Declaring iterator std :: string :: iterator it ; // Declaring reverse iterator std :: string :: reverse_iterator it1 ; cout < < 'Str:' < < str < < ' n ' ; // Displaying string cout < < 'The string using forward iterators is : ' ; for ( it = str . begin (); it != str . end (); it ++ ){ if ( it == str . begin ()) * it = 'G' ; cout < < * it ; } cout < < endl ; str = 'geeksforgeeks' ; // Displaying reverse string cout < < 'The reverse string using reverse iterators is ' ': ' ; for ( it1 = str . rbegin (); it1 != str . rend (); it1 ++ ){ if ( it1 == str . rbegin ()) * it1 = 'S' ; cout < < * it1 ; } cout < < endl ; str = 'geeksforgeeks' ; //Displaying String cout < < 'The string using constant forward iterator is :' ; for ( auto it2 = str . cbegin (); it2 != str . cend (); it2 ++ ){ //if(it2 == str.cbegin()) *it2='G'; //here modification is NOT Possible //error: assignment of read-only location //As it is a pointer to the const content but we can inc/dec-rement the iterator cout < <* it2 ; } cout < < ' n ' ; str = 'geeksforgeeks' ; //Displaying String in reverse cout < < 'The reverse string using constant reverse iterator is :' ; for ( auto it3 = str . crbegin (); it3 != str . crend (); it3 ++ ){ //if(it2 == str.cbegin()) *it2='S'; //here modification is NOT Possible //error: assignment of read-only location //As it is a pointer to the const content but we can inc/dec-rement the iterator cout < <* it3 ; } cout < < ' n ' ; return 0 ; } //Code modified by Balakrishnan R (rbkraj000)
出力
Str:geeksforgeeks The string using forward iterators is : Geeksforgeeks The reverse string using reverse iterators is : Skeegrofskeeg The string using constant forward iterator is :geeksforgeeks The reverse string using constant reverse iterator is :skeegrofskeeg
時間計算量: O(1)
空間の複雑さ: O(n) ここで、n は文字列のサイズです
4) 関数の操作:
| 関数 | 意味 |
|---|---|
| copy('char 配列' len pos) | この関数は、引数で指定されたターゲット文字配列内の部分文字列をコピーします。コピーされるターゲット char 配列の長さとコピーを開始する文字列内の開始位置の 3 つの引数を取ります。 |
| スワップ() | この関数は、ある文字列を別の文字列と交換します。 |
例:
CPP // C++ Program to demonstrate the working of // copy() and swap() #include #include // for string class using namespace std ; // Driver Code int main () { // Initializing 1st string string str1 = 'geeksforgeeks is for geeks' ; // Declaring 2nd string string str2 = 'geeksforgeeks rocks' ; // Declaring character array char ch [ 80 ]; // using copy() to copy elements into char array // copies 'geeksforgeeks' str1 . copy ( ch 13 0 ); // Displaying char array cout < < 'The new copied character array is : ' ; cout < < ch < < endl ; // Displaying strings before swapping cout < < 'The 1st string before swapping is : ' ; cout < < str1 < < endl ; cout < < 'The 2nd string before swapping is : ' ; cout < < str2 < < endl ; // using swap() to swap string content str1 . swap ( str2 ); // Displaying strings after swapping cout < < 'The 1st string after swapping is : ' ; cout < < str1 < < endl ; cout < < 'The 2nd string after swapping is : ' ; cout < < str2 < < endl ; return 0 ; }
出力
The new copied character array is : geeksforgeeks The 1st string before swapping is : geeksforgeeks is for geeks The 2nd string before swapping is : geeksforgeeks rocks The 1st string after swapping is : geeksforgeeks rocks The 2nd string after swapping is : geeksforgeeks is for geeks
必読: C++ 文字列クラスとそのアプリケーション