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
);
參數
參數 |
描述 |
_Val |
要插入之項目的值至 hash_map,除非 hash_map 已經包含該項目 (或,一般而言,索引鍵相等的已排序的項目)。 |
_Where |
如需這個位置開始的提示搜尋正確問題的外掛程式。 |
_First |
從 hash_map 將複製的第一個項目的位置。 |
_Last |
位置是從 hash_map 將複製的最後一個項目之外。 |
傳回值
第一個 insert 成員函式傳回 bool 元件傳回 true 的配對,如果插入是進行錯誤的,如果 hash_map 已經包含索引鍵具有等值的順序,因此, Iterator 元件傳回位址或插入新項目的項目已經放置項目。
若要存取一組 pr 的 Iterator 元件 (此成員函式傳回,使用 pr。first和解除參考它,請使用* (pr。first)。 若要存取一組 pr 的 bool 元件 (此成員函式傳回,使用 pr。second和解除參考它,請使用* (pr。second)。
第二個 insert 成員函式,指向新位置的項目插入 hash_map 的提示版本,傳回 Iterator。
最後兩個 insert 成員函式一般作業的前兩個相同,不過,它們移動建構插入的值。
備註
項目的 value_type 是一組,因此元素,的值會與第一個元件相等於這個機碼值和第二個元件的已排序配對相等於這個項目之資料值。
如果插入點後面緊接著 _Where,插入提示版本的已轉換舊的常數時間可能發生,而不是對的時間。
第三 + 成成員函式插入項目值序列 hash_map 與範圍 Iterator 定址每個項目對應到 [First, Last) 的最後一個指定的集合。
範例
// 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