备注
此 API 已过时。替代为 unordered_map 类。
从指定位置移除元素或范围在 hash_map 的元素或移除与指定键的元素。
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);
参数
_Where
从 hash_map 中移除元素的位置。_First
从 hash_map 移除的第一个元素的位置。_Last
除了从 hash_map 移除的最后一个元素。确定。_Key
从 hash_map 要移除的元素的键值。
返回值
对于最后两个成员函数、将保持。所有元素外的第一个元素。移除的双向迭代器或 hash_map 到的末尾。指针,如果不存在这样的元素。
对于第三个成员函数,返回从 hash_map 中移除元素的数目。
备注
成员函数不引发异常。
在Visual C++ .NET 2003中,成员<hash_map> 和 <hash_set> 头文件不再在std命名空间,而是已经进入了stdext命名空间。 有关更多信息,请参见 stdext 命名空间。
示例
在编译此示例与 /Wp64 标志或在 64 位平台时,编译器将生成警告 C4267。 有关该警告的更多信息,请参见 编译器警告(等级 3)C4267。
// hash_map_erase.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main()
{
using namespace std;
using namespace stdext;
hash_map<int, int> hm1, hm2, hm3;
hash_map<int, int> :: iterator pIter, Iter1, Iter2;
int i;
hash_map<int, int>::size_type n;
typedef pair<int, int> Int_Pair;
for (i = 1; i < 5; i++)
{
hm1.insert(Int_Pair (i, i));
hm2.insert(Int_Pair (i, i*i));
hm3.insert(Int_Pair (i, i-1));
}
// The 1st member function removes an element at a given position
Iter1 = ++hm1.begin();
hm1.erase(Iter1);
cout << "After the 2nd element is deleted, the hash_map hm1 is:";
for (pIter = hm1.begin(); pIter != hm1.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
// The 2nd member function removes elements
// in the range [_First, _Last)
Iter1 = ++hm2.begin();
Iter2 = --hm2.end();
hm2.erase(Iter1, Iter2);
cout << "After the middle two elements are deleted, "
<< "the hash_map hm2 is:";
for (pIter = hm2.begin(); pIter != hm2.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
// The 3rd member function removes elements with a given _Key
n = hm3.erase(2);
cout << "After the element with a key of 2 is deleted,\n"
<< "the hash_map hm3 is:";
for (pIter = hm3.begin(); pIter != hm3.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
// The 3rd member function returns the number of elements removed
cout << "The number of elements removed from hm3 is: "
<< n << "." << endl;
// The dereferenced iterator can also be used to specify a key
Iter1 = ++hm3.begin();
hm3.erase(Iter1);
cout << "After another element with a key equal to that"
<< endl;
cout << "of the 2nd element is deleted, "
<< "the hash_map hm3 is:";
for (pIter = hm3.begin(); pIter != hm3.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
}
要求
标头: <hash_map>
**命名空间:**stdext