C++ の文字列

C++ の文字列

C++ 文字列は、char 配列に格納された文字のシーケンスです。文字列は単語やテキストを保存するために使用されます。また、数値やその他の種類の情報などのデータを保存するためにも使用されます。 C++ の文字列は、次のいずれかを使用して定義できます。 std::string クラス または C スタイルの文字配列。

C++ の文字列

1. C スタイルの文字列

これらの文字列は、 ヌル文字「 」 。これらは、C++ が C 言語から継承した文字列のタイプです。

構文:

char str[] = 'techcodeview.com'; 

例:

C++




// C++ Program to demonstrate strings> #include> using> namespace> std;> int> main()> {> > char> s[] => 'techcodeview.com'> ;> > cout < < s < < endl;> > return> 0;> }>

出力

techcodeview.com 

2. std::string クラス

これらは、ヘッダー ファイル内で定義される std::string クラスとして C++ に導入された新しいタイプの文字列です。これにより、動的なサイズ、メンバー関数など、従来の C スタイルの文字列に比べて多くの利点が得られます。

構文:

std::string str('techcodeview.com'); 

例:

C++




// C++ program to create std::string objects> #include> using> namespace> std;> int> main()> {> > string str(> 'techcodeview.com'> );> > cout < < str;> > return> 0;> }>

出力

techcodeview.com 

もう 1 つの方法では、同じ文字が何度も繰り返される文字列を作成できます。

構文:

std::string str(number,character); 

例:

C++




#include> using> namespace> std;> int> main()> {> > string str(5,> 'g'> );> > cout < < str;> > return> 0;> }>

出力:

ggggg 

C++ で文字列を定義する方法

C++ では、文字列をいくつかの方法で定義できます。文字列には、string クラスを使用して標準ライブラリからアクセスできます。文字配列を使用して文字列を定義することもできます。 String は、検索や操作など、一般的に使用されるメソッドの豊富な機能セットを提供します。このメソッドは string クラスよりも高度ではありませんが、より効率的で使いやすいため、依然として広く使用されています。 C++ で文字列を定義する方法は次のとおりです。

  • 文字列キーワードの使用
  • C スタイルの文字列の使用

1. 文字列キーワードの使用

array キーワードを使用する代わりに string キーワードを使用して文字列を定義するほうが、記述と理解が簡単なので便利です。

構文:

string s = 'techcodeview.com'; string s('techcodeview.com'); 

例:

C++




// C++ Program to demonstrate use of string keyword> #include> using> namespace> std;> int> main()> {> > string s => 'techcodeview.com'> ;> > string str(> 'techcodeview.com'> );> > cout < <> 's = '> < < s < < endl;> > cout < <> 'str = '> < < str < < endl;> > return> 0;> }>

出力

s = techcodeview.com str = techcodeview.com 

2. C スタイルの文字列の使用

strcpy()、strcmp()、strcat() などの C スタイルの文字列ライブラリ関数を使用して文字列を定義します。この方法はより複雑で、他の 2 つほど広く使用されていませんが、レガシー コードを扱う場合やパフォーマンスが必要な場合に便利です。

char s[] = {'g', 'f', 'g', ' '}; char s[4] = {'g', 'f', 'g', ' '}; char s[4] = 'gfg'; char s[] = 'gfg'; 

例:

C++




// C++ Program to demonstrate C-style string declaration> #include> using> namespace> std;> int> main()> {> > char> s1[] = {> 'g'> ,> 'f'> ,> 'g'> ,> ' '> };> > char> s2[4] = {> 'g'> ,> 'f'> ,> 'g'> ,> ' '> };> > char> s3[4] => 'gfg'> ;> > char> s4[] => 'gfg'> ;> > cout < <> 's1 = '> < < s1 < < endl;> > cout < <> 's2 = '> < < s2 < < endl;> > cout < <> 's3 = '> < < s3 < < endl;> > cout < <> 's4 = '> < < s4 < < endl;> > return> 0;> }>

出力

s1 = gfg s2 = gfg s3 = gfg s4 = gfg 

C スタイルの文字列の別の例:

C++




#include> using> namespace> std;> int> main()> {> > string S => 'Geeeks for Geeks'> ;> > cout < <> 'Your string is= '> ;> > cout < < S < < endl;> > return> 0;> }>

出力

Your string is= Geeeks for Geeks 

C++ で文字列入力を取得する方法

文字列入力とは、ユーザーから文字列を受け入れることを意味します。 C++ の場合。文字列に応じて、ユーザーからの入力を受け取るさまざまなタイプがあります。最も一般的な方法は、次のように入力を取得することです。 食べる C++ の抽出演算子 (>>) を使用したキーワード。文字列を入力として受け取るメソッドは次のとおりです。

  • 食べる
  • ゲットライン
  • ストリングストリーム

1.Cinの使用

文字列入力を取得する最も簡単な方法は、 食べる コマンドをストリーム抽出演算子 (>>) とともに使用します。

構文:

cin>>s;> 

例:

C++




// C++ Program to demonstrate string input using cin> #include> using> namespace> std;> int> main() {> > string s;> > > cout < <> 'Enter String'> < cin>>s; コート < <'String is: ' < return 0; }>

出力

Enter String String is: 

出力:

Enter String techcodeview.com String is: techcodeview.com 

2. getline の使用

C++ の getline() 関数 入力ストリームから文字列を読み取るために使用されます。ヘッダファイル内で宣言されます。

構文:

getline(cin,s); 

例:

C++




// C++ Program to demonstrate use of getline function> #include> using> namespace> std;> int> main()> {> > string s;> > cout < <> 'Enter String'> < < endl;> > getline(cin, s);> > cout < <> 'String is: '> < < s < < endl;> > return> 0;> }>

出力

Enter String String is: 

出力:

Enter String techcodeview.com String is: techcodeview.com 

3. 文字列ストリームの使用

の文字列ストリームクラス C++ は、複数の文字列を一度に入力として受け取るために使用されます。

構文:

stringstream stringstream_object(string_name); 

例:

C++




// C++ Program to demonstrate use of stringstream object> #include> #include> #include> using> namespace> std;> int> main()> {> > string s => ' techcodeview.com to the Moon '> ;> > stringstream obj(s);> > // string to store words individually> > string temp;> > //>> 演算子は stringstream オブジェクトから読み取ります> > while> (obj>> 温度) {>> > cout < < temp < < endl;> > }> > return> 0;> }>

出力

techcodeview.com to the Moon 

文字列を関数に渡すにはどうすればよいですか?

配列を関数に渡すのと同じ方法で、C++ の文字列を文字配列として関数に渡すことができます。以下にプログラム例を示します。

例:

C++




// C++ Program to print string using function> #include> using> namespace> std;> void> print_string(string s)> {> > cout < <> 'Passed String is: '> < < s < < endl;> > return> ;> }> int> main()> {> > string s => 'techcodeview.com'> ;> > print_string(s);> > return> 0;> }>

出力

Passed String is: techcodeview.com 

ポインタと文字列

C++ のポインターは、アドレスの記号表現です。これらにより、プログラムは参照による呼び出しをシミュレートしたり、動的なデータ構造を作成および操作したりすることができます。ポインタを使用すると、文字列の開始アドレスである文字列の最初の文字を取得できます。以下に示すように、ポインターを介して指定された文字列にアクセスし、出力することができます。

例:

C++




// C++ Program to print string using pointers> #include> using> namespace> std;> int> main()> {> > string s => 'Geeksforgeeks'> ;> > // pointer variable declared to store the starting> > // address of the string> > char> * p = &s[0];> > // this loop will execute and print the character till> > // the character value is null this loop will execute and> > // print the characters> > while> (*p !=> ' '> ) {> > cout < < *p;> > p++;> > }> > cout < < endl;> > return> 0;> }>

出力

Geeksforgeeks 

C++ における文字列配列と文字配列の違い

文字列と文字配列の主な違いは、文字列は不変であるのに対し、文字配列は不変ではないことです。

文字配列

文字列は、文字列ストリームとして表現できるオブジェクトを定義します。 NULL 文字は、文字の文字配列を終了します。
文字列はオブジェクトとして表現されるため、文字列内で配列の減衰は発生しません。

の脅威

配列の減衰

文字配列の場合に存在します

文字列クラスは、文字列を操作するための多数の関数を提供します。 文字配列には、文字列を操作するための組み込み関数がありません。
メモリは動的に割り当てられます。 文字配列のサイズは静的に割り当てる必要があります。

について詳しく知る C++ における文字列と文字配列の違い

C++ 文字列関数

C++ には、文字列のコピーや連結を行う strcpy() 関数や strcat() 関数など、文字列操作に使用される組み込み関数がいくつか用意されています。そのうちのいくつかは次のとおりです。

関数

説明

長さ() この関数は文字列の長さを返します。
スワップ() この関数は、2 つの文字列の値を交換するために使用されます。
サイズ() 文字列のサイズを調べるために使用されます
サイズ変更() この関数は、指定された文字数まで文字列の長さを変更するために使用されます。
探す() パラメータで渡される文字列を検索するために使用されます
プッシュバック() この関数は、渡された文字を文字列の末尾にプッシュするために使用されます。
ポップバック() この関数は、文字列から最後の文字をポップするために使用されます。
クリア() この関数は、文字列のすべての要素を削除するために使用されます。
strncmp() この関数は、渡された両方の文字列の最大で最初の num バイトを比較します。
strncpy() この関数は strcpy() 関数と似ていますが、src の最大 n バイトがコピーされる点が異なります。
strrchr() この関数は、文字列内で最後に出現する文字を見つけます。
割れた() この関数は、ソース文字列のコピーを宛先文字列の末尾に追加します。
探す() この関数は、文字列内の特定の部分文字列を検索するために使用され、部分文字列の最初の文字の位置を返します。
交換する() この関数は、古い値と等しい範囲 [first, last) 内の各要素を新しい値に置き換えるのに使用されます。
部分文字列() この関数は、指定された文字列から部分文字列を作成するために使用されます。
比較する() この関数は 2 つの文字列を比較するために使用され、結果を整数の形式で返します。
消去() この関数は、文字列の特定の部分を削除するために使用されます。

C++ 文字列イテレータ関数

C++ では、組み込みの文字列イテレータ関数により、プログラマは文字列要素を変更したり走査したりする簡単な方法が提供されます。これらの関数は次のとおりです。

機能 説明
始める() この関数は、文字列の先頭を指す反復子を返します。
終わり() この関数は、文字列の末尾を指す反復子を返します。
rfind() この関数は、文字列の最後の出現箇所を検索するために使用されます。
rbegin() この関数は、文字列の末尾を指す逆反復子を返します。
与える() この関数は、文字列の先頭を指す逆反復子を返します。
cbegin() この関数は、文字列の先頭を指す const_iterator を返します。
いくつか() この関数は、文字列の末尾を指す const_iterator を返します。
crbegin() この関数は、文字列の末尾を指す const_reverse_iterator を返します。
クレンド() この関数は、文字列の先頭を指す const_reverse_iterator を返します。

例:

C++




// C++ Program to demonstrate string iterator functions> #include> using> namespace> std;> int> main()> {> > // declaring an iterator> > string::iterator itr;> > // declaring a reverse iterator> > string::reverse_iterator rit;> > string s => 'techcodeview.com'> ;> > itr = s.begin();> > > cout < <> 'Pointing to the start of the string: '> < < *itr < < endl;> > itr = s.end() - 1;> > cout < <> 'Pointing to the end of the string: '> < < *itr < < endl;> > rit = s.rbegin();> > cout < <> 'Pointing to the last character of the string: '> < < *rit < < endl;> > rit = s.rend() - 1;> > cout < <> 'Pointing to the first character of the string: '> < < *rit < < endl;> > return> 0;> }>

出力

Pointing to the start of the string: G Pointing to the end of the string: s Pointing to the last character of the string: s Pointing to the first character of the string: G 

文字列容量関数

C++ では、文字列容量関数を使用して文字列のサイズと容量を管理します。容量の主な機能は次のとおりです。

関数 説明
長さ() この関数は文字列のサイズを返すために使用されます。
容量() この関数は、コンパイラによって文字列に割り当てられた容量を返します。
サイズ変更() この関数を使用すると、文字列のサイズを増減できます。
シュリンクトゥフィット() この機能は容量を減らして最小値に等しくします。

例:

C++




#include> using> namespace> std;> int> main()> {> > string s => 'techcodeview.com'> ;> > > // length function is used to print the length of the string> > cout < <> 'The length of the string is '> < < s.length() < < endl;> > > // capacity function is used to print the capacity of the string> > cout < <> 'The capacity of string is '> < < s.capacity() < < endl;> > > // the string.resize() function is used to resize the string to 10 characters> > s.resize(10);> > > cout < <> 'The string after using resize function is '> < < s < < endl;> > > s.resize(20);> > > cout < <> 'The capacity of string before using shrink_to_fit function is '> < < s.capacity() < < endl;> > > // shrink to fit function is used to reduce the capacity of the container> > s.shrink_to_fit();> > cout < <> 'The capacity of string after using shrink_to_fit function is '> < < s.capacity() < < endl;> > return> 0;> }>

出力

The length of the string is 13 The capacity of string is 15 The string after using resize function is GeeksforGe The capacity of string before using shrink_to_fit function is 30 The capacity of string... 

結論として、この記事では、文字配列と文字列クラスを使用して C++ で文字列を定義する方法について説明します。文字列クラスはより高度な機能を提供し、文字配列は基本的な機能を提供しますが、効率的で使いやすいです。この記事では、ユーザーからの入力を取得するためのさまざまな方法についても説明しました。

std::string クラスの詳細については、記事を参照してください。 C++ の std::string クラス