共用方式為


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
    解決的 Iterator 後續字元要插入的位置。

  • _First
    處理輸入 const_pointer Iterator、或的 const_iterator 要在中插入的來源範圍中第一個項目。

  • _Last
    處理輸入、或 const_pointer Iterator 的位置 const_iterator 該在最後一個項目以外要在中插入的來源範圍。

傳回值

在個別字元插入的情況下,您可以在成員函式指定處理新的字元,或者, 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