共用方式為


basic_string::operator+=

將字元附加至字串。

basic_string<CharType, Traits, Allocator>& operator+=(
   value_type _Ch
);
basic_string<CharType, Traits, Allocator>& operator+=(
   const value_type* _Ptr
);
basic_string<CharType, Traits, Allocator>& operator+=(
   const basic_string<CharType, Traits, Allocator>& _Right
);

參數

  • _Ch
    要附加的字元。

  • _Ptr
    要附加至的 C 字串中的字元。

  • _Right
    要附加的字串中的字元。

傳回值

在附加至成員函式傳遞的字元的字串物件的參考。

備註

使用 operator+= 或成員函式 附加push_back,字元可附加至字串。 operator+= 附加單一引數值,以多個引數附加函式允許字串的特定部分將指定成員時。

範例

// basic_string_op_app.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;

   // The first member function
   // appending a single character to a string
   string str1a ( "Hello" );
   cout << "The original string str1 is: " << str1a << endl;
   str1a +=  '!' ;
   cout << "The string str1 appended with an exclamation is: " 
        << str1a << endl << endl;

   // The second member function
   // appending a C-string to a string
   string  str1b ( "Hello " );
   const char *cstr1b = "Out There";
   cout << "The C-string cstr1b is: " << cstr1b << endl;
   str1b +=  cstr1b;
   cout << "Appending the C-string cstr1b to string str1 gives: " 
        << str1b << "." << endl << endl;

   // The third member function
   // appending one string to another in two ways,
   // comparing append and operator [ ]
   string str1d ( "Hello " ), str2d ( "Wide " ), str3d ( "World" );
   cout << "The string str2d is: " << str2d << endl;
   str1d.append ( str2d );
   cout << "The appended string str1d is: " 
        << str1d << "." << endl;
   str1d += str3d;
   cout << "The doubly appended strig str1 is: " 
        << str1d << "." << endl << endl;
}
  
  
  
  

需求

標題: <string>

命名空間: std

請參閱

參考

basic_string Class