共用方式為


string::operator+

說明如何使用 string::operator + STL Visual C++ 中的函式。

template<class _E, class _TYPE, class _A> inline
   basic_string<_E, _TYPE, _A>
   operator+( 
      const basic_string<_E, _TYPE, _A>& LString,
      const _E *RCharArray 
   );
template<class _E, class _TYPE, class _A> inline
   basic_string<_E, _TYPE, _A>
   operator+( 
      const _E *LCharArray,
      const basic_string<_E, _TYPE, _A>& RString 
   );
template<class _E, class _TYPE, class _A> inline
   basic_string<_E, _TYPE, _A>
   operator+( 
      const basic_string<_E, _TYPE, _A>& LString,
      const _E RChar 
   );
template<class _E, class _TYPE, class _A> inline
   basic_string<_E, _TYPE, _A>
   operator+( 
      const _E LChar,
      const basic_string<_E, _TYPE, _A>& RString 
   );
template<class _E, class _TYPE, class _A> inline
   basic_string<_E, _TYPE, _A>
   operator+( 
      const basic_string<_E, _TYPE, _A>& LString,
      const basic_string<_E, _TYPE, _A>& RString 
   );

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

有五個版本的 string::operator + 函式。 兩個函式用來串連 null 結尾字元陣列和 basic_string。 兩個函式用來串連的字元和 basic_string。 最後一個函式用來串連兩個 basic_string 變數。

範例

// StringPlus.cpp
// compile with: /EHsc
// Illustrates how to use the operator+ to concatenate
// a null-terminated character array and a basic_string,
// how to concatenate a character and a basic_string,
// and how to concatenate two basic_string variables.
//
// Functions:
//
//    operator+ : Concatenates a null-terminated character array and
//                a basic_string.
//    operator+ : Concatenates a character array and a basic_string.
//    operator+ : Concatenates two basic_string variables.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

int main()
{
   string result;
   string S1="ABC";
   string S2="DEF";
   char CP1[]="GHI";
   char C='J';

   cout << "S1 is " << S1 << endl;
   cout << "S2 is " << S2 << endl;
   cout << "CP1 is " << CP1 << endl;
   cout << "C is " << C << endl;

   result=S1+CP1;                           // Function 1  (ABCGHI)
   cout << "S1+CP1 is " << result << endl;

   result=CP1+S1;                           // Function 2  (GHIABC)
   cout << "CP1+S1 is " << result << endl;

   result=S1+S2;                            // Function 3  (ABCDEF)
   cout << "S1+S2 is " << result << endl;

   result=S1+C;                             // Function 4  (ABCJ)
   cout << "S1+C is " << result << endl;

   result=C+S1;                             // Function 5  (JABC)
   cout << "C+S1 is " << result << endl;
}
  

需求

標頭: <string>

請參閱

概念

標準樣板程式庫範例