C ++ map delete()函數(shù)用于從map容器中刪除與給定鍵值關(guān)聯(lián)的單個元素或元素范圍。因此,將通過刪除元素的數(shù)量來減小尺寸。
void erase (iterator position); // 在 C++ 11 之前 size_type erase (const key_type& k); // 在 C++ 11 之前 void erase (iterator first, iterator last); // 在 C++ 11 之前 iterator erase (const_iterator position); //從 C++ 11 開始 size_type erase (const key_type& k); //從 C++ 11 開始 iterator erase (const_iterator first, const_iterator last); //從 C++ 11 開始
position:指向要從map中刪除的單個元素的迭代器。
k:要從map上移除的元素的鍵。
first:要擦除范圍的開始。
last:要擦除范圍的末尾。
它返回一個指向已刪除元素的下一個元素的迭代器,或者返回已刪除元素的數(shù)量。
讓我們看一個簡單的示例,通過迭代器擦除元素。
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
cout<<"刪除元素之前: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
it=mymap.find('b');
mymap.erase (it); // erasing by iterator
cout<<"\n刪除元素后: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}輸出:
刪除元素之前: a => 10 b => 20 c => 30 d => 40 刪除元素后: a => 10 c => 30 d => 40
在上面的示例中,元素被迭代器擦除。
讓我們看一個簡單的示例,以給定的鍵值擦除map的元素。
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
cout<<"刪除元素之前: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
mymap.erase ('c'); // erasing by key
cout<<"\n刪除元素后: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}輸出:
刪除元素之前: a => 10 b => 20 c => 30 d => 40 刪除元素后: a => 10 b => 20 d => 40
在上面的示例中,delete(key)函數(shù)使用鍵值'c'及其映射中的值。
讓我們看一個簡單的示例,以給定范圍擦除元素。
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
cout<<"刪除元素之前: \n";
cout<<"Size大小: "<<mymap.size()<<'\n';
for (it=mymap.begin(); it!=mymap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
mymap.erase ( mymap.begin () , mymap.end () ); // erasing by range
cout<<"\n刪除元素后: \n";
cout<<"Size大小: "<<mymap.size();
for (it=mymap.begin(); it!=mymap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
return 0;
}輸出:
刪除元素之前: Size is: 4 a => 10 b => 20 c => 30 d => 40 刪除元素后: Size is: 0
在上面的示例中,使用了delete(first,last)函數(shù)來擦除具有給定范圍(即開始到結(jié)束)的元素。
讓我們看一個簡單的示例,從map中刪除所有奇數(shù)。
#include <map>
#include <iostream>
using namespace std;
int main()
{
map<int, string> m = {{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"}};
// 從m刪除所有奇數(shù)
cout<<"刪除奇數(shù)后,元素是:\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.second << ", ";
}輸出:
刪除奇數(shù)后,元素是: two, four, six,
在上面的示例中,所有奇數(shù)均已刪除,并顯示偶數(shù)。