basic_string::insert

插入元素或多个元素或元素的大小为字符串中的指定位置。

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

参数

  • _P0
    位置的索引。问题的后插入新字符。

  • _Ptr
    的C字符串全部或部分插入到字符串。

  • _Count
    要插入的字符数。

  • _Str
    的字符串全部或部分插入到目标字符串。

  • _Off
    源字符串的索引提供的字符追加。

  • _Ch
    要插入的元素的字符值。

  • _It
    解决的迭代器字符后要插入的位置。

  • _First
    解决输入迭代器、const_pointer或的const_iterator在要插入的源范围的第一个元素。

  • _Last
    解决输入迭代器、const_pointer或的const_iterator位置于最后一个元素外在要插入的源范围。

返回值

用于按成员函数分配解决新字符;或者在各个字符插入,迭代器插入的字符位置的字符串对象的引用则返回none,按特殊成员函数。

示例

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

要求

标头: <string>

命名空间: std

请参见

参考

basic_string Class