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