Partager via


basic_string::operator+=

Ajoute des caractères dans une chaîne.

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
);

Paramètres

  • _Ch
    Le caractère à ajouter.

  • _Ptr
    Les caractères de la chaîne c à ajouter.

  • _Right
    Les caractères de la chaîne à ajouter.

Valeur de retour

Une référence à l'objet chaîne qui est ajouté par les caractères est passée par la fonction membre.

Notes

Les caractères peuvent être ajoutés à une chaîne à l'aide de operator+= ou les fonctions membres ajoutez ou push_back.operator+= ajoute des valeurs d'argument unique lorsque plusieurs arguments ajoutez la fonction membre permet une partie spécifique d'une chaîne à spécifier pour ajouter.

Exemple

// 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;
}
  
  
  
  

Configuration requise

en-tête : <string>

l'espace de noms : DST

Voir aussi

Référence

basic_string Class