C++ 多映射 erase() 函數用於從多映射容器中刪除與給定鍵值關聯的單個元素或一係列元素。因此,大小將因刪除的元素數量而減少。
用法
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:Iterator 指向要從多重映射中刪除的單個元素。
k: 要從多重映射中刪除的元素的鍵。
first:要擦除的範圍的開始。
last:要擦除的範圍結束。
返回值
它返回一個指向已刪除元素的下一個元素的迭代器或返回已刪除元素的數量。
複雜度
擦除(位置):攤銷常數。
擦除(val):容器大小的對數。
擦除(第一個,最後一個):第一個和最後一個之間的距離是線性的。
迭代器有效性
迭代器、引用和指向被函數刪除的元素的指針無效。
所有其他迭代器、指針和引用保持其有效性。
數據競爭
容器被修改。
刪除的元素被修改。盡管同時訪問其他元素是安全的,但在容器中迭代範圍並不安全。
異常安全
該函數不會拋出異常。
如果指定了無效的範圍或位置,則會導致未定義的行為。
例子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
在上麵的例子中,元素被迭代器 it 擦除。
例子2
讓我們看一個簡單的例子,用給定的鍵值擦除 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
在上麵的例子中,erase(key) 函數從多重映射中擦除所有的鍵 'b' 及其值。
例子3
讓我們看一個簡單的例子來擦除給定範圍內的元素:
#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
在上麵的例子中,erase (first, last) 函數用於擦除給定範圍內的元素,即開始到結束。
示例 4
讓我們看一個簡單的例子來擦除多重映射中的所有奇數:
#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
在上麵的例子中,所有的奇數都被擦除了,顯示的是偶數。
相關用法
- C++ multimap empty()用法及代碼示例
- C++ multimap emplace()用法及代碼示例
- C++ multimap equal_range()用法及代碼示例
- C++ multimap end()用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
- C++ multimap cend()用法及代碼示例
- C++ multimap insert()用法及代碼示例
- C++ multimap lower_bound()用法及代碼示例
- C++ multimap rend()用法及代碼示例
- C++ multimap maxsize()用法及代碼示例
- C++ multimap rend用法及代碼示例
- C++ multimap get_allocator()用法及代碼示例
- C++ multimap upper_bound()用法及代碼示例
- C++ multimap clear()用法及代碼示例
- C++ multimap crend()用法及代碼示例
- C++ multimap crbegin()用法及代碼示例
- C++ multimap key_comp用法及代碼示例
- C++ multimap rbegin用法及代碼示例
- C++ multimap rbegin()用法及代碼示例
- C++ multimap value_comp()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ multimap erase() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。