Share via


multimap::insert

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

iterator insert(
   const value_type& _Val
);
iterator insert(
   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 multimap unless the multimap already contains that element or, more generally, an element whose key is equivalently ordered.

_Where

A hint regarding the place to start searching for the correct point of insertion.

_First

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

_Last

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

Return Value

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

Remarks

The value_type of an element is a pair, so that the value of an element will be an ordered pair with the first component equal to the key value and the second component equal to the data value of the element.

Insertion can occur in amortized constant time for the hint version of insert, instead of logarithmic time, if the insertion point immediately follows _Where.

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

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

Example

// multimap_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;
   multimap <int, int>::iterator m1_pIter, m2_pIter;

   multimap <int, int> m1, m2;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   cout << "The original key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "." << endl;

   cout << "The original mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m1.insert ( Int_Pair ( 1, 10 ) );

   // The hint version of insert
   m1.insert( --m1.end( ), Int_Pair ( 4, 40 )  );

   cout << "After the insertions, the key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "," << endl;

   cout << " and the mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m2.insert ( Int_Pair ( 10, 100 ) );

   // The templatized version inserting a range
   m2.insert( ++m1.begin( ), --m1.end( ) );

   cout << "After the insertions, the key values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> first;
   cout << "," << endl;

   cout << " and the mapped values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> second;
   cout << "." << endl;

    // The templatized versions move constructing elements
    multimap<int, string> m3, m4;
    pair<int, string> is1(1, "a"), is2(2, "b");

    m3.insert(move(is1));
    cout << "After the move insertion, m3 contains:" << endl
      << " " << m3.begin()->first
      << " => " << m3.begin()->second
      << endl;

    m4.insert(c4.begin(),move(is2));
    cout << "After the move insertion, m4 contains:" << endl
      << " " << m4.begin()->first
      << " => " << m4.begin()->second
      << endl;
}
The original key values of m1 = 1 2 3.
The original mapped values of m1 = 10 20 30.
After the insertions, the key values of m1 = 1 1 2 3 4,
 and the mapped values of m1 = 10 10 20 30 40.
After the insertions, the key values of m2 = 1 2 3 10,
 and the mapped values of m2 = 10 20 30 100.
After the move insertion, m3 contains:
 1 => a
After the move insertion, m4 contains:
 2 => b

Requirements

Header: <map>

Namespace: std

See Also

Reference

multimap Class

Standard Template Library

Other Resources

multimap Members