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 除元素外或最后将插入的源区。

返回值

为给由成员函数分配处理字符位置的新字符,或者在各个字符插入,迭代器插入的字符串对象的引用或是中,根据无特殊成员函数。

示例

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

要求

标头:< 字符串>

命名空间: std

请参见

参考

basic_string 类