Share via


unordered_multimap::insert

Adds elements.

iterator insert(const value_type& val);
iterator insert(iterator where, const value_type& val);
template<class InIt>
    void insert(InIt first, InIt last);

Parameters

  • InIt
    The iterator type.

  • first
    Beginning of range to insert.

  • last
    End of range to insert.

  • val
    Value to insert.

  • where
    Where in container to insert (hint only).

Remarks

The first member function inserts the element val in the controlled sequence, then returns the iterator that designates the inserted element. The second member function returns insert(val), using where as a starting place within the controlled sequence to search for the insertion point. (Insertion can possibly occur somewhat faster, if the insertion point immediately precedes or follows where.) The third member function inserts the sequence of element values, for each where in the range [first, last), by calling insert(*where).

If an exception is thrown during the insertion of a single element, the container is left unaltered and the exception is rethrown. If an exception is thrown during the insertion of multiple elements, the container is left in a stable but unspecified state and the exception is rethrown.

Example

 

// std_tr1__unordered_map__unordered_multimap_insert.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
 
typedef std::tr1::unordered_multimap<char, int> Mymap; 
int main() 
    { 
    Mymap c1; 
 
    c1.insert(Mymap::value_type('a', 1)); 
    c1.insert(Mymap::value_type('b', 2)); 
    c1.insert(Mymap::value_type('c', 3)); 
 
// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
// insert with hint and reinspect 
    Mymap::iterator it2 = c1.insert(c1.begin(), Mymap::value_type('d', 4)); 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
// insert range and inspect 
    Mymap c2; 
 
    c2.insert(c1.begin(), c1.end()); 
    for (Mymap::const_iterator it = c2.begin(); 
        it != c2.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
// insert new and duplicate, and reinspect 
    c1.insert(Mymap::value_type('e', 5)); 
    c1.insert(Mymap::value_type('a', 6)); 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    return (0); 
    } 
 

[c, 3] [b, 2] [a, 1] [d, 4] [c, 3] [b, 2] [a, 1] [d, 4] [c, 3] [b, 2] [a, 1] [e, 5] [d, 4] [c, 3] [b, 2] [a, 1] [a, 6]

Requirements

Header: <unordered_map>

Namespace: std::tr1

See Also

Reference

<unordered_map>

unordered_multimap Class