list::insert

插入項目或一些元素或某個範圍的元素中輸入清單的指定位置。

iterator insert(
   const_iterator _Where,
   const Type& _Val
);
iterator insert(
   const_iterator _Where,
   Type&& _Val
);
void insert(
   iterator _Where,
   size_type _Count,
   const Type& _Val
);
template<class InputIterator>
   void insert(
      iterator _Where,
      InputIterator _First,
      InputIterator _Last
   );

參數

參數

描述

_Where

在第一個項目插入的目標清單的位置。

_Val

要插入至清單項目的值。

_Count

要插入至清單中的項目數目。

_First

第一個項目的位置包含在項目範圍內的中要複製的引數清單。

_Last

第一個項目的位置在的元素範圍會在複製的引數清單。

傳回值

前兩個 insert 函式傳回 Iterator 為新的項目插入清單的位置取代該點。

範例

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

int main( ) 
{
   using namespace std;
   list <int> c1, c2;
   list <int>::iterator Iter;
   
   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c2.push_back( 40 );
   c2.push_back( 50 );
   c2.push_back( 60 );

   cout << "c1 =";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;

   Iter = c1.begin( );
   Iter++;
   c1.insert( Iter, 100 );
   cout << "c1 =";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;

   Iter = c1.begin( );
   Iter++;
   Iter++;
   c1.insert( Iter, 2, 200 );

   cout << "c1 =";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;

   c1.insert( ++c1.begin( ), c2.begin( ),--c2.end( ) );

   cout << "c1 =";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;

// initialize a list of strings by moving
   list < string > c2;
   string str("a");

   c2.insert( c2.begin(), move( str ) );
   cout << "Moved first element: " << c2.front( ) << endl;
}

Output

c1 = 10 20 30
c1 = 10 100 20 30
c1 = 10 100 200 200 20 30
c1 = 10 40 50 100 200 200 20 30
Moved first element: a

需求

標題: <list>

命名空間: std

請參閱

參考

list Class

list::insert (STL Samples)

標準樣板程式庫