hash_set::erase
Примечание
Этот API устарел.Альтернативой является Класс unordered_set.
Удаляет элемент или набор элементов в hash_set из заданных позиций или удалять элементы, соответствующие указанному ключу.
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);
Параметры
_Where
Положение элемента, необходимо удалить из hash_set._First
Позиция первого элемента удалила из hash_set._Last
Позиция только за последним элементом удалила из hash_set._Key
Ключ элемента, который необходимо удалить из hash_set.
Возвращаемое значение
Для первых 2 функций-членов, двунаправленного итератора, обозначает первый элемент оставшиеся за всеми удаленными элементами, или указателя на конец hash_set, если такой элемент не существует. Для третьего функцию-член, количество элементов, которые были удалены из hash_set.
Заметки
Функции-члены не генерируют исключений.
В Visual C++ .NET 2003 C, элементы файла заголовка <hash_map> и <hash_set> больше не в пространстве имен std, а перемещается в пространство имен stdext. Дополнительные сведения см. в разделе Пространство имен stdext.
Пример
При компилировании этого образца с отметкой /Wp64 или на 64 разрядной платформе, создается предупреждение компилятора C4267. Дополнительные сведения об этом предупреждении см. в разделе Предупреждение компилятора (уровень 3) C4267.
// hash_set_erase.cpp
// compile with: /EHsc
#include <hash_set>
#include <iostream>
int main()
{
using namespace std;
using namespace stdext;
hash_set<int> hs1, hs2, hs3;
hash_set<int>::iterator pIter, Iter1, Iter2;
int i;
hash_set<int>::size_type n;
for (i = 1; i < 5; i++)
{
hs1.insert (i);
hs2.insert (i * i);
hs3.insert (i - 1);
}
// The 1st member function removes an element at a given position
Iter1 = ++hs1.begin();
hs1.erase(Iter1);
cout << "After the 2nd element is deleted, the hash_set hs1 is:";
for (pIter = hs1.begin(); pIter != hs1.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
// The 2nd member function removes elements
// in the range [_First, _Last)
Iter1 = ++hs2.begin();
Iter2 = --hs2.end();
hs2.erase(Iter1, Iter2);
cout << "After the middle two elements are deleted, "
<< "the hash_set hs2 is:";
for (pIter = hs2.begin(); pIter != hs2.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
// The 3rd member function removes elements with a given _Key
n = hs3.erase(2);
cout << "After the element with a key of 2 is deleted, "
<< "the hash_set hs3 is:";
for (pIter = hs3.begin(); pIter != hs3.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
// The 3rd member function returns the number of elements removed
cout << "The number of elements removed from hs3 is: "
<< n << "." << endl;
// The dereferenced iterator can also be used to specify a key
Iter1 = ++hs3.begin();
hs3.erase(Iter1);
cout << "After another element (unique for hash_set) with a key "
<< endl;
cout << "equal to that of the 2nd element is deleted, "
<< "the hash_set hs3 is:";
for (pIter = hs3.begin(); pIter != hs3.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
}
Требования
Заголовок:<hash_set>
Пространство имен: stdext