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