移除指定位置处的元素。
iterator erase(iterator where);
iterator erase(iterator first, iterator last);
bool erase(key_type key)
参数
first
清除的范围开头。键
要清除的键值。last
清除的范围末尾。where
要清除的元素。
备注
第一移除成员函数控制序列的元素由 where指向,并返回指定保持了元素外的第一个元素。移除的迭代器,或者 map::end (STL/CLR)(),如果不存在这样的元素。 使用其删除一个元素。
第二个成员中移除函数控制元素在序列的范围的 [first, last),并返回指定保持超过所有元素外的第一个元素。移除的迭代器,或 end(),如果不存在这样的元素。 您移除它使用零个或更多连续的元素。
第三个成员函数移除键有等效排序到 key控件序列的所有元素,并返回元素的数目的计数中移除。 使用它、移除和计数与指定键的所有元素。
每个元素需要消除时间比例元素数为底的对数控制在序列中。
示例
// cliext_map_erase.cpp
// compile with: /clr
#include <cliext/map>
typedef cliext::map<wchar_t, int> Mymap;
int main()
{
cliext::map<wchar_t, int> c1;
c1.insert(cliext::map<wchar_t, int>::make_value(L'a', 1));
c1.insert(cliext::map<wchar_t, int>::make_value(L'b', 2));
c1.insert(cliext::map<wchar_t, int>::make_value(L'c', 3));
// display contents " [a 1] [b 2] [c 3]"
for each (cliext::map<wchar_t, int>::value_type elem in c1)
System::Console::Write(" [{0} {1}]", elem->first, elem->second);
System::Console::WriteLine();
// erase an element and reinspect
cliext::map<wchar_t, int>::iterator it =
c1.erase(c1.begin());
System::Console::WriteLine("erase(begin()) = [{0} {1}]",
it->first, it->second);
// add elements and display " b c d e"
c1.insert(cliext::map<wchar_t, int>::make_value(L'd', 4));
c1.insert(cliext::map<wchar_t, int>::make_value(L'e', 5));
for each (cliext::map<wchar_t, int>::value_type elem in c1)
System::Console::Write(" [{0} {1}]", elem->first, elem->second);
System::Console::WriteLine();
// erase all but end
it = c1.end();
it = c1.erase(c1.begin(), --it);
System::Console::WriteLine("erase(begin(), end()-1) = [{0} {1}]",
it->first, it->second);
System::Console::WriteLine("size() = {0}", c1.size());
// erase end
System::Console::WriteLine("erase(L'x') = {0}", c1.erase(L'x'));
System::Console::WriteLine("erase(L'e') = {0}", c1.erase(L'e'));
return (0);
}
要求
标头: <cliext/map>
命名空间: cliext