C++multimap erase()函数用于从multimap集合中删除与给定键值关联的单个元素或一系列元素。因此,将通过删除元素的数量来减小尺寸。
void erase (iterator position); //until C++ 11 size_type erase (const key_type& k); //until C++ 11 void erase (iterator first, iterator last); //until C++ 11 iterator erase (const_iterator position); //since C++ 11 size_type erase (const key_type& k); //since C++ 11 iterator erase (const_iterator first, const_iterator last); //since C++ 11
position - 迭代器,指向要从multimap中删除的单个元素。
k - 要从多重映射中删除的元素的键。
first - 要擦除的参数的开始。
last - 要擦除的参数的结尾。
它返回一个指向已删除元素的下一个元素的迭代器,或者返回已删除元素的数量。erase - 例子1
让我们看一个简单的示例,该示例通过迭代器擦除元素。
#include <iostream> #include <map> using namespace std; int main () { multimap<char,int> mymultimap; multimap<char,int>::iterator it; mymultimap= { {'a', 100}, {'b', 200}, {'b', 300}, {'c', 400} }; cout<<"Before erasing the element: \n"; for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; it=mymultimap.find('b'); mymultimap.erase (it); //erasing by iterator cout<<"\nAfter erasing the element: \n"; for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
输出:
Before erasing the element: a => 100 b => 200 b => 300 c => 400 After erasing the element: a => 100 b => 300 c => 400
在上面的示例中,元素被迭代器擦除。
让我们看一个简单的示例,用给定的键值擦除multimap的元素:
#include <iostream> #include <map> using namespace std; int main () { multimap<char,int> mymultimap; multimap<char,int>::iterator it; mymultimap = { {'a', 100}, {'b', 200}, {'b', 300}, {'c', 400} }; cout<<"Before erasing the element: \n"; for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; mymultimap.erase ('b'); //erasing by key cout<<"\nAfter erasing the element: \n"; for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
输出:
Before erasing the element: a => 100 b => 200 b => 300 c => 400 After erasing the element: a => 100 c => 400
在上面的示例中,delete(key)函数从多重映射中删除所有键" b"及其值。
让我们看一个简单的示例,以给定参数擦除元素:
#include <iostream> #include <map> using namespace std; int main () { multimap<char,int> mymultimap; multimap<char,int>::iterator it; mymultimap = { {'a', 100}, {'b', 200}, {'b', 300}, {'c', 400} }; cout<<"Before erasing the element are: \n"; cout<<"Size is: "<<mymultimap.size()<<'\n'; for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) cout << it->first << " => " << it->second << '\n'; mymultimap.erase ( mymultimap.begin () , mymultimap.end () ); //erasing by range cout<<"\nAfter erasing the element are: \n"; cout<<"Size is: "<<mymultimap.size(); for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) cout << it->first << " => " << it->second << '\n'; return 0; }
输出:
Before erasing the element are: Size is: 4 a => 100 b => 200 b => 300 c => 400 After erasing the element are: Size is: 0
让我们看一个简单的示例,从多重映射中删除所有奇数:
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-erase-function.html
来源:LearnFk无涯教程网
#include <map> #include <iostream> using namespace std; int main() { multimap<int, string> m = { {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"} }; //erase all odd numbers from m cout<<"After erasing odd numbers,elements are:\n "; for(auto it = m.begin(); it != m.end(); ) if(it->first % 2 == 1) it = m.erase(it); else ++it; for(auto& p : m) cout <<p.first <<", "<< p.second << "\n "; }
输出:
After erasing odd numbers, elements are: 2, two 4, four 6, six
在上面的示例中,所有奇数均已删除,并显示偶数。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕