Share via


unordered_map::insert

Adds elements.

std::pair<iterator, bool> insert(const value_type& val);
iterator insert(iterator where, const value_type& val);
template<class InIt>
    void insert(InIt first, InIt last);
template<class ValTy>
    pair<iterator, bool> insert(ValTy&& val);
template<class ValTy>
    iterator insert(const_iterator where, ValTy&& val);

Parameters

Parameter

Description

InIt

The iterator type.

ValTy

The in-place constructor argument 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 determines whether an element X exists in the sequence whose key has equivalent ordering to that of val. If not, it creates such an element X and initializes it with val. The function then determines the iterator where that designates X. If an insertion occurred, the function returns std::pair(where, true). Otherwise, it returns std::pair(where, false).

The second member function returns insert(val).first, 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).

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

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_map_insert.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
#include <string>
 
typedef std::unordered_map<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 with checking and reinspect 
    std::pair<Mymap::iterator, bool> pib = 
        c1.insert(Mymap::value_type('e', 5)); 
    std::cout << "insert(['a', 5]) success == " 
        << std::boolalpha << pib.second << std::endl; 
    pib = c1.insert(Mymap::value_type('a', 6)); 
    std::cout << "insert(['a', 5]) success == " 
        << std::boolalpha << pib.second << std::endl; 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 

// The templatized versions move constructing elements
    unordered_map<int, string> c3, c4;
    pair<int, string> is1(1, "a"), is2(2, "b");

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

    c4.insert(c4.begin(), std::move(is2));
    std::cout << "After the move insertion, c4 contains:" << std::endl
      << " " << c4.begin()->first
      << " => " << c4.begin()->second
      << 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]
insert(['a', 5]) success == true
insert(['a', 5]) success == false
 [e, 5] [d, 4] [c, 3] [b, 2] [a, 1]
After the move insertion, c3 contains:
 1 => a
After the move insertion, c4 contains:
 2 => b

Requirements

Header: <unordered_map>

Namespace: std

See Also

Reference

<unordered_map>

unordered_map Class

Other Resources

<unordered_map> Members