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

必要条件

ヘッダー: の <文字列>

名前空間: std

参照

関連項目

basic_string クラス