C++ での文字列の連結
文字列は、文字を格納するために使用されるデータ構造の一種です。 C++ での文字列の連結は、文字列に関連して最もよく議論されるトピックの 1 つです。ユーザー定義のメソッドを使用して文字列を連結する方法は複数あり、事前定義のメソッドを使用して文字列を連結する方法もいくつかあります。これらすべての方法を確認してみましょう。
文字列を連結するメソッド
連結には6つの方法があります 弦 以下に述べるように:
- append()関数を使用します。
- 「+」演算子を使用します。
- strcat()関数を使用します。
- C++ for ループを使用します。
- 継承の使用。
- Friend 関数と strcat() 関数の使用。
1. append()関数の使用
append() 関数は、 std::string クラス 。この関数を使用すると、次の例に示すように 2 つの std::string オブジェクト (C++ スタイルの文字列) を連結できます。
構文:
string& string::append (const string& str); Here, str: String to be appended.
以下は、append() 関数を使用した文字列連結の C++ プログラムです。
C++
// C++ Program for string> // concatenation using append> #include> using> namespace> std;> // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > // Appending the string.> > init.append(add);> > cout < < init < < endl;> > return> 0;> }> |
出力
this is init added now
2.「+」演算子の使用
これは 2 つの文字列を連結する最も簡単な方法です。の + オペレーター 文字列を追加します そして連結された文字列を返します。このメソッドは、C++ スタイルの文字列 (std::string オブジェクト) に対してのみ機能し、C スタイルの文字列 (文字配列) に対しては機能しません。
構文:
string new_string = init + add;
以下は、「+」演算子を使用した文字列連結の C++ プログラムです。
C++
// C++ Program for string> // concatenation using '+' operator> #include> using> namespace> std;> // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > // Appending the string.> > init = init + add;> > cout < < init < < endl;> > return> 0;> }> |
出力
this is init added now
3. strcat()関数の使用
C++ strcat( ) 関数は、以下で定義された組み込み関数です。 ヘッダファイル。この関数は 2 つの文字列を連結します。 熱 そして 追加 そして結果はに保存されます 熱 弦。この関数は、C スタイルの文字列 (文字配列) に対してのみ機能し、C++ スタイルの文字列 (std::string オブジェクト) に対しては機能しません。
構文:
char * strcat(char * init, const char * add);
以下は、strcat() 関数を使用した文字列連結の C++ プログラムです。
C++
// C++ Program for string> // concatenation using strcat> #include> #include> using> namespace> std;> // Driver code> int> main()> {> > char> init[] => 'this is init'> ;> > char> add[] => ' added now'> ;> > // Concatenating the string.> > strcat> (init, add);> > cout < < init < < endl;> > return> 0;> }> |
出力
this is init added now
4. for ループの使用
ループの使用は、文字列連結の最も基本的な方法の 1 つです。ここでは、文字列全体を走査し、次に別の文字列を走査しながら、要素を 1 つずつ追加しています。最終結果は、両方の文字列から形成された連結された文字列になります。
以下は、for ループを使用した文字列連結の C++ プログラムです。
C++
// C++ Program for string> // concatenation using for loop> #include> using> namespace> std;> // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > string output;> > // Adding element inside output> > // from init> > for> (> int> i = 0; init[i] !=> ' '> ; i++)> > {> > output += init[i];> > }> > // Adding element inside output> > // fromt add> > for> (> int> i = 0; add[i] !=> ' '> ; i++)> > {> > output += add[i];> > }> > cout < < output < < endl;> > return> 0;> }> |
出力
this is init added now
5. 継承の使用
以下は、継承を使用した文字列連結の C++ プログラムです。
C++
// C++ program for string concatenation> // using inheritance> #include> #include> using> namespace> std;> > // Base class> class> base> {> > protected> :> > virtual> string concatenate(string &str1,> > string &str2) = 0;> };> > // Derive class> class> derive:> protected> base {> > public> :> > string concatenate (string &str1,> > string &str2)> > {> > string temp;> > temp = str1 + str2;> > return> temp;> > }> };> > // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > > // Create string object> > derive obj;> > > // Print string> > cout < < obj.concatenate (init, add);> > > return> 0;> }> |
出力
this is init added now
6. Friend関数とstrcat()関数の使用
以下は、friend 関数と strcat() 関数を使用した文字列連結の C++ プログラムです。
C++
// C++ program for string concatenation> // using friend function and strcat()> #include> #include> using> namespace> std;> // Base class> class> Base {> > public> :> > char> init[100] => 'this is init'> ;> > char> add[100] => ' added now'> ;> > > friend> void> myfun(Base b);> };> > void> myfun (Base b)> {> > // Pass parameter to concatenate> > strcat> (b.init, b.add);> > > cout < < b.init;> }> // Driver code> int> main()> {> > > // Create object of base class> > Base b;> > > // pass b object to myfun() to print> > // the concatenated string> > myfun(b);> > > return> 0;> }> |
出力
this is init added now