unordered_multimap::insert

新增項目。

iterator 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>
    iterator insert(ValTy&& val);
template<class ValTy>
    iterator insert(const_iterator where, ValTy&& val);

參數

參數

描述

InIt

Iterator 型別。

ValTy

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

First

若要插入的範圍的開頭。

Last

若要插入的範圍的結尾。

Val

若要插入的值。

Where

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

備註

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

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

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

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

範例

// std_tr1__unordered_map__unordered_multimap_insert.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
#include <string>
 
typedef std::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; 

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

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

    c4.insert(c4.begin(), move(is2));
    cout << "After the move insertion, c4 contains:" << endl
      << " " << c4.begin()->first
      << " => " << c4.begin()->second
      << endl;
 
    return (0); 
    } 
 
  

需求

標頭: <unordered_map>

Namespace: 標準

請參閱

參考

<unordered_map>

unordered_multimap Class

其他資源

<unordered_map> 成員