Partager via


multiset::insert

Inserts an element or a range of elements into a multiset.

iterator insert(
   const value_type& _Val
);
iterator insert(
   const_iterator _Where,
   const value_type& _Val
);
template<class InputIterator>
   void insert(
      InputIterator _First,
      InputIterator _Last
   );
template<class ValTy>
    iterator insert(
        ValTy&& val
);
template<class ValTy>
    iterator insert(
        const_iterator _Where,
        ValTy&& _Val
);

Parameters

Parameter

Description

_Val

The value of an element to be inserted into the multiset unless the multiset already contains that element or, more generally, an element whose key is equivalently ordered.

_Where

The place to start searching for the correct point of insertion. Insertion can occur in amortized constant time instead of logarithmic time, if the insertion point immediately follows _Where.

_First

The position of the first element to be copied from a multiset.

_Last

The position just beyond the last element to be copied from a multiset.

Return Value

The insert member functions returns an iterator that points to the position where the new element was inserted into the multiset.

Remarks

The third member function inserts the sequence of element values into a multiset corresponding to each element addressed by an iterator in the range [_First, _Last) of a specified multiset.

The last two member functions behave the same as the first two, except that val is used to construct the inserted value.

Example

// multiset_insert.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;
   multiset <int>::iterator ms1_pIter, ms2_pIter;

   multiset <int, less<int> > ms1, ms2;
   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );
   ms1.insert( 40 );

   cout << "The original ms1 =";
   for ( ms1_pIter = ms1.begin( ); ms1_pIter != ms1.end( ); ms1_pIter++ )
      cout << " " << *ms1_pIter;
   cout << "." << endl;

   ms1.insert( 20 );
   ms1.insert( --ms1.end( ), 50 );

   cout << "After the insertions, ms1 =";
   for ( ms1_pIter = ms1.begin( ); ms1_pIter != ms1.end( ); ms1_pIter++ )
      cout << " " << *ms1_pIter;
   cout << "." << endl;

   ms2.insert( 100 );
   ms2.insert( ++ms1.begin( ), --ms1.end( ) );

   cout << "ms2 =";
   for ( ms2_pIter = ms2.begin( ); ms2_pIter != ms2.end( ); ms2_pIter++ )
      cout << " " << *ms2_pIter;
   cout << "." << endl;

   // Construct by moving
   multiset<string> ms3, ms4;
   string str1("a"), str2("b");

   ms3.insert(move(str1));
   cout << "After the move insertion, ms3 contains: "
      << *ms3.begin() << endl;

   ms4.insert(ms4.begin(), move(str2));
   cout << "After the move insertion, ms4 contains: "
      << *ms4.begin() << endl;
}
The original ms1 = 10 20 30 40.
After the insertions, ms1 = 10 20 20 30 40 50.
ms2 = 20 20 30 40 100.
After the move insertion, ms3 contains: a
After the move insertion, ms4 contains: b

Requirements

Header: <set>

Namespace: std

See Also

Reference

multiset Class

Standard Template Library

Other Resources

multiset Members