Поделиться через


hash_map::erase

ПримечаниеПримечание

Этот API устарел.Альтернативы unordered_map Class.

Удаляет элемент или диапазон элементов в 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.

Возвращаемое значение

Для первых 2 функций-членов, двухнаправленного итератора, который задает первый элемент оставшиеся за всеми удаленными элементами или указателя на конец 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 пространство имен:

См. также

Ссылки

hash_map Class

Стандартная библиотека шаблонов