basic_string::insert

Fügt ein Element oder mehrere Elemente oder ein Bereich von Elementen in die Zeichenfolge in einer angegebenen Position ein.

basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0, 
   const value_type* _Ptr
);
basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0, 
   const value_type* _Ptr,
   size_type _Count
);
basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0,
   const basic_string<CharType, Traits, Allocator>& _Str
);
basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0,
   const basic_string<CharType, Traits, Allocator>& _Str, 
   size_type _Off, 
   size_type _Count
);
basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0,
   size_type _Count, 
   value_type _Ch
);
iterator insert(
   iterator _It
);
iterator insert(
   iterator _It,
   value_type _Ch
)l
template<class InputIterator>
   void insert(
      iterator _It, 
      InputIterator _First, 
      InputIterator _Last
   );
void insert(
   iterator _It, 
   size_type _Count, 
   value_type _Ch
);
void insert(
   iterator _It,
   const_pointer _First,
   const_pointer _Last
);
void insert(
   iterator _It,
   const_iterator _First,
   const_iterator _Last
);

Parameter

  • _P0
    Der Index der Position nach dem Punkt Einfügen die neuen Zeichen.

  • _Ptr
    Die in die Zeichenfolge ganz oder teilweise eingefügt werden C-Zeichenfolge.

  • _Count
    Die Anzahl der einzufügenden Zeichen.

  • _Str
    Die in der Zielzeichenfolge ganz oder teilweise einzufügende Zeichenfolge.

  • _Off
    Der Index des Teils der Quellzeichenfolge die angefügt werden Zeichen angibt.

  • _Ch
    Der Zeichenwert der eingefügt werden Elemente.

  • _It
    Ein Iterator, der die Position hinter abweicht, der ein Zeichen eingefügt werden soll.

  • _First
    Ein Eingabeiterator, ein const_pointer oder ein const_iterator, das das erste Element im eingefügt werden Quellbereich behandelt.

  • _Last
    Ein Eingabeiterator, ein const_pointer oder ein const_iterator, das die Position von der über dem letzten Element hinaus im Quellbereich behandelt eingefügt werden.

Rückgabewert

Ein Verweis auf ein Zeichenfolgenobjekt, das die neuen Zeichen durch die Memberfunktion oder, im Falle der einzelnen Zeicheneinfügungen, einen Iterator zugewiesen wird, welche, die Position des eingefügten Zeichens beheben bzw. keine, je nach bestimmten Memberfunktion.

Beispiel

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

int main( )
{
   using namespace std;

   // The first member function inserting a C-string
   // at a given position
   basic_string <char> str1a ( "way" );
   const char *cstr1a = "a";
   str1a.insert ( 0, cstr1a );
   cout << "The string with a C-string inserted at position 0 is: "
        << str1a << "." << endl;

   // The second member function inserting a C-string
   // at a given position for a specified number of elements
   basic_string <char> str2a ( "Good" );
   const char *cstr2a = "Bye Bye Baby";
   str2a.insert ( 4, cstr2a ,3 );
   cout << "The string with a C-string inserted at the end is: "
        << str2a << "." << endl;

   // The third member function inserting a string
   // at a given position
   basic_string <char> str3a ( "Bye" );
   string str3b ( "Good" );
   str3a.insert ( 0, str3b );
   cout << "The string with a string inserted at position 0 is: "
        << str3a << "." << endl;

   // The fourth member function inserting part of
   // a string at a given position
   basic_string <char> str4a ( "Good " );
   string str4b ( "Bye Bye Baby" );
   str4a.insert ( 5, str4b , 8 , 4 );
   cout << "The string with part of a string inserted at position 4 is: "
        << str4a << "." << endl;


   // The fifth member function inserts a number of characters
   // at a specified position in the string
   string str5 ( "The number is: ." );
   str5.insert ( 15 , 3 , '3' );
   cout << "The string with characters inserted is: "
        << str5 << endl;

   // The sixth member function inserts a character
   // at a specified position in the string
   string str6 ( "ABCDFG" );
   basic_string <char>::iterator str6_Iter = ( str6.begin ( ) + 4 );
   str6.insert ( str6_Iter , 'e' );
   cout << "The string with a character inserted is: "
        << str6 << endl;

   // The seventh member function inserts a range
   // at a specified position in the string
   string str7a ( "ABCDHIJ" );
   string str7b ( "abcdefgh" );
   basic_string <char>::iterator str7a_Iter = (str7a.begin ( ) + 4 );
   str7a.insert ( str7a_Iter , str7b.begin ( ) + 4 , str7b.end ( ) -1 );
   cout << "The string with a character inserted from a range is: "
        << str7a << endl;

   // The eigth member function inserts a number of
   // characters at a specified position in the string
   string str8 ( "ABCDHIJ" );
   basic_string <char>::iterator str8_Iter = ( str8.begin ( ) + 4 );
   str8.insert ( str8_Iter , 3 , 'e' );
   cout << "The string with a character inserted from a range is: "
        << str8 << endl;
}
  
  
  
  
  
  

Anforderungen

Header: <string>

Namespace: std

Siehe auch

Referenz

basic_string Class