共用方式為


unordered_map::insert

新增項目。

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);

參數

參數

描述

InIt

Iterator 型別。

ValTy

在就地的建構函式的引數型別。

first

若要插入的範圍的開頭。

last

若要插入的範圍的結尾。

val

若要插入的值。

where

若要插入 (只有提示) 的容器中的位置。

備註

第一個成員函式會判斷是否項目X存在的索引鍵有對等排序的順序val。 如果不是,它會建立這類項目X ,並初始化它的val。 此函式接著會判斷 iterator where來指定X。 如果插入就會發生,則函數會傳回std::pair(where, true)。 否則,會傳回 std::pair(where, false)。

第二個成員函式會傳回insert(val).first、 使用where為起始的位置中搜尋插入點的受控制序列。 (插入可能可能速度會較快,如果插入點立即前面或後面是where。)

第三個成員函式每個插入的項目值的順序where範圍內[first, last),藉由呼叫insert(*where)。

最後兩個成員函式的行為與前兩個,除了的相同val用來建構插入的值。

如果單一元素的插入動作期間擲回例外狀況時,就會留在容器不變,和例外狀況重新擲回。 如果插入多個項目時擲回例外狀況時,容器會處於穩定但未指定的狀態,並會重新擲回例外狀況。

範例

// 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); 
    } 
 
  

需求

標頭: <unordered_map>

Namespace: 標準

請參閱

參考

<unordered_map>

unordered_map Class

其他資源

<unordered_map> 成員