hash_map::insert

备注

此 API 已过时。替代为 unordered_map 类

插入元素或某一范围元素到 hash_map。

pair <iterator, bool> insert(
    const value_type& _Val
);
iterator insert(
    const_iterator _Where,
    const value_type& _Val
);
template<class InputIterator>
    void insert(
        InputIterator _First,
        InputIterator _Last
);
template<class ValTy>
    pair <iterator, bool> insert(
        ValTy&& _Val
);
template<class ValTy>
    iterator insert(
        const_iterator _Where,
        ValTy&& _Val
);

参数

参数

说明

_Val

将插入的元素的值到 hash_map,除非已 hash_map (或包含该元素,通常,该键相同顺序排列) 的元素。

_Where

有关起始位置的提示搜索正确位置插入。

_First

从 hash_map 将要复制的第一个元素的位置。

_Last

位置仅从 hash_map 将最后复制的一个元素外。

返回值

第一 插入 成员函数返回布尔组件返回 true 的对,则插入是使和错误的,则 hash_map 已包含了具有等效值,键顺序,以及迭代器组件返回地址的新元素插入或的元素已经位于的位置。

若要访问受对 pr 的迭代器组件由该成员函数返回,则使用 pr。first和取消引用它,使用* (pr。first)。 若要访问受对 pr 的 bool 组件成员由此函数返回,则使用 pr。和取消引用它,使用* (pr。)。

第二 插入 成员函数,会提示生成,返回指向新位置的元素插入 hash_map 的迭代器。

最后两 插入 成员函数行为与前两个相同,只不过前者,将构造插入的值。

备注

value_type 一个元素是一对, 使元素值为一个有序对,第一个组件与键值相同和第二组件与元素数据值相同。

(假如插入点紧跟_Where,插入将发生在常量级时间内而不出逻辑时间内)。

第三个成员函数插入元素值序列到 hash_map 对应于迭代范围器解决的每个元素对应 。首先,最后) 指定的集合。

示例

// hash_map_insert.cpp
// compile with: /EHsc
#include<hash_map>
#include<iostream>
#include <string>

int main()
{
    using namespace std;
    using namespace stdext;
    hash_map<int, int>::iterator hm1_pIter, hm2_pIter;

    hash_map<int, int> hm1, hm2;
    typedef pair<int, int> Int_Pair;

    hm1.insert(Int_Pair(1, 10));
    hm1.insert(Int_Pair(2, 20));
    hm1.insert(Int_Pair(3, 30));
    hm1.insert(Int_Pair(4, 40));

    cout<< "The original elements (Key => Value) of hm1 are:";
    for (hm1_pIter = hm1.begin(); hm1_pIter != hm1.end(); hm1_pIter++)
        cout << endl << " " << hm1_pIter -> first << " => "
             << hm1_pIter->second;
    cout << endl;

    pair< hash_map<int,int>::iterator, bool > pr;
    pr = hm1.insert(Int_Pair(1, 10));

    if (pr.second == true)
    {
        cout<< "The element 10 was inserted in hm1 successfully."
            << endl;
    }
    else
    {
        cout<< "The element 10 already exists in hm1\n with a key value of"
            << "((pr.first) -> first)= "<<(pr.first)-> first
            << "."<< endl;
    }

    // The hint version of insert
    hm1.insert(--hm1.end(), Int_Pair(5, 50));

    cout<< "After the insertions, the elements of hm1 are:";
    for (hm1_pIter = hm1.begin(); hm1_pIter != hm1.end(); hm1_pIter++)
        cout << endl << " " << hm1_pIter -> first << " => "
             << hm1_pIter->second;
    cout << endl;

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

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

    cout<< "After the insertions, the elements of hm2 are:";
    for (hm2_pIter = hm2.begin(); hm2_pIter != hm2.end(); hm2_pIter++)
        cout << endl << " " << hm2_pIter -> first << " => "
             << hm2_pIter->second;
    cout << endl;

    // The templatized versions move constructing elements
    hash_map<int, string> hm3, hm4;
    pair<int, string> is1(1, "a"), is2(2, "b");

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

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

要求

标头: <hash_map>

**命名空间:**stdext

请参见

参考

hash_map 类

标准模板库