hash_map::insert

说明说明

此 API 已过时。另一种方法是 unordered_map Class

插入元素或元素的范围。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
);

参数

Parameter

描述

_Val

要插入的元素的值设置为 hash_map,除非 hash_map 已包含该元素 (或,通常,键相同地排序) 的元素。

_Where

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

_First

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

_Last

在从 hash_map 将复制的最后一个元素之外。

返回值

第一个 插入 成员函数返回 bool 元素返回 true 的对,如果插入是使和错误的,如果 hash_map 已经包含一个键具有等效值顺序,并且,迭代器元素返回地址插入或新元素的元素已找到其中的元素。

为对迭代器元素 pr 由该成员函数返回的访问,请使用 pr。第一个和取消引用它,使用* (pr。第一个)。为对 bool 元素 pr 由该成员函数返回的访问,请使用 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 Class

标准模板库