![]() |
---|
此 API 已过时。另一种方法是 unordered_multimap Class。 |
插入元素或元素的范围。hash_multimap 中。
iterator 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>
iterator insert(
ValTy&& _Val
);
template<class ValTy>
iterator insert(
const_iterator _Where,
ValTy&& _Val
);
参数
Parameter |
描述 |
_Val |
要插入的元素的值设置为 hash_multimap,除非 hash_multimap,通常,已包含该元素或键相同地排序的元素。 |
_Where |
有关起始位置的提示搜索正确位置插入。 |
_First |
从映射将复制的第一个元素的位置。 |
_Last |
位置在从映射将复制的最后一个元素之外。 |
返回值
前两个 插入 成员函数返回指向位置插入新元素的迭代器。
最后两个 插入 成员函数的行为与前两个相同,不同之处在于,它们将构造该插入的值。
备注
元素的 value_type 是对,因此,元素的值将排序的匹配的第一个元素等于键值和第二个元素为等于元素的数据值。
如果插入点紧跟在 _Where,在插入的提示版本中能够对常数的时间可能发生,而不是对数时间。
元素顺序值添加到映射的每个元素对应的第三成员函数插入由迭代器在范围解析的 [_First,_Last) 的指定设置。
示例
// hash_multimap_insert.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
#include <string>
int main( )
{
using namespace std;
using namespace stdext;
hash_multimap <int, int>::iterator hm1_pIter, hm2_pIter;
hash_multimap <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 ) );
cout << "The original key values of hm1 =";
for ( hm1_pIter = hm1.begin( ); hm1_pIter != hm1.end( );
hm1_pIter++ )
cout << " " << hm1_pIter -> first;
cout << "." << endl;
cout << "The original mapped values of hm1 =";
for ( hm1_pIter = hm1.begin( ); hm1_pIter != hm1.end( );
hm1_pIter++ )
cout << " " << hm1_pIter -> second;
cout << "." << endl;
hm1.insert ( Int_Pair ( 1, 10 ) );
// The hint version of insert
hm1.insert( --hm1.end( ), Int_Pair ( 4, 40 ) );
cout << "After the insertions, the key values of hm1 =";
for ( hm1_pIter = hm1.begin( ); hm1_pIter != hm1.end( );
hm1_pIter++ )
cout << " " << hm1_pIter -> first;
cout << "," << endl;
cout << " and the mapped values of hm1 =";
for ( hm1_pIter = hm1.begin( ); hm1_pIter != hm1.end( );
hm1_pIter++ )
cout << " " << 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 key values of hm2 =";
for ( hm2_pIter = hm2.begin( ); hm2_pIter != hm2.end( );
hm2_pIter++ )
cout << " " << hm2_pIter -> first;
cout << "," << endl;
cout << " and the mapped values of hm2 =";
for ( hm2_pIter = hm2.begin( ); hm2_pIter != hm2.end( );
hm2_pIter++ )
cout << " " << hm2_pIter -> second;
cout << "." << endl;
// The templatized versions move constructing elements
hash_multimap<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