unordered_map::erase
移除指定位置的項目。
iterator erase(iterator where);
iterator erase(iterator first, iterator last);
size_type erase(const Key& keyval);
參數
first
要清除之範圍的開頭。key
要清除的索引鍵值。last
要清除之範圍的結尾。where
要清除的項目
備註
第 10% 成員函式移除受控制序列中所指向的 where。 第二 + 成成員函式移除在這個範圍 [first, last)的項目。 傳回兩個指定保持在所有項目外的第一個項目中移除或 unordered_map::end() 的 Iterator,如果沒有此類項目存在則為。
第三個成員移除 unordered_map::equal_range分隔範圍中的項目(keyval)。 它會將移除的項目數目。
成員函式絕不會擲回例外狀況。
範例
// std_tr1__unordered_map__unordered_map_erase.cpp
// compile with: /EHsc
#include <unordered_map>
#include <iostream>
typedef std::unordered_map<char, int> Mymap;
int main()
{
Mymap c1;
c1.insert(Mymap::value_type('a', 1));
c1.insert(Mymap::value_type('b', 2));
c1.insert(Mymap::value_type('c', 3));
// display contents " [c 3] [b 2] [a 1]"
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// erase an element and reinspect
Mymap::iterator it2 = c1.erase(c1.begin());
std::cout << "*erase(begin()) == ["
<< it2->first << ", " << it2->second << "]";
std::cout << std::endl;
// add elements and display " [e 5] [d 4] [b 2] [a 1]"
c1.insert(Mymap::value_type('d', 4));
c1.insert(Mymap::value_type('e', 5));
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// erase all but end;
it2 = c1.end();
it2 = c1.erase(c1.begin(), --it2);
std::cout << "*erase(begin(), end()-1) == ["
<< it2->first << ", " << it2->second << "]" << std::endl;
std::cout << "size() == " << c1.size() << std::endl;
return (0);
}
需求
標題: <unordered_map>
命名空間: std