先決條件:多集
multiset::erase()是C++中的STL函數,它從多集中刪除指定的元素。
此方法有三個版本。這些是:
-
用法:
void erase (iterator position_of_iterator);
參數:此方法接受以下參數:
- position_of_iterator:它指的是在迭代器的幫助下要刪除的特定元素的位置。
返回值:此方法返回被刪除元素之後的迭代器。
以下示例說明了multiset::erase()方法的用法方式:
// C++ program to demonstrate // multiset::erase() method #include <bits/stdc++.h> using namespace std; int main() { // Initialise the multiset multiset<int> multi_set; multiset<int>::iterator ms_iterator; // Add values to the multiset for (int i = 1; i < 10; i++) { multi_set.insert(i); } cout << "Original multiset: "; for (ms_iterator = multi_set.begin(); ms_iterator != multi_set.end(); ++ms_iterator) cout << ' ' << *ms_iterator; cout << '\n'; ms_iterator = multi_set.begin(); ms_iterator++; // Passing the iterator for the position // at which the value is to be erased multi_set.erase(ms_iterator); cout << "Modified multiset: "; for (ms_iterator = multi_set.begin(); ms_iterator != multi_set.end(); ++ms_iterator) cout << ' ' << *ms_iterator; cout << '\n'; return 0; }
輸出:Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 3 4 5 6 7 8 9
-
用法:
size_type erase (const value_type& contant_value);
參數:此方法接受以下參數:
- constant_value:指的是借助其值從多重集中刪除的特定元素。它必須是恒定的。此方法將擦除此值的所有實例。
返回值:此方法返回no。被刪除的值。
以下示例說明了multiset::erase()方法的用法方式:
// C++ program to demonstrate // multiset::erase() method #include <bits/stdc++.h> using namespace std; int main() { // Initialise the multiset multiset<int> multi_set; multiset<int>::iterator ms_iterator; // Add values to the multiset for (int i = 1; i < 10; i++) { multi_set.insert(i); } cout << "Original multiset: "; for (ms_iterator = multi_set.begin(); ms_iterator != multi_set.end(); ++ms_iterator) cout << ' ' << *ms_iterator; cout << '\n'; ms_iterator = multi_set.begin(); // Passing constant value to be erased int num = multi_set.erase(2); cout << "Modified multiset: " << "(" << num << ")" << "removed"; for (ms_iterator = multi_set.begin(); ms_iterator != multi_set.end(); ++ms_iterator) cout << ' ' << *ms_iterator; cout << '\n'; return 0; }
輸出:
Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset:(1)removed 1 3 4 5 6 7 8 9
-
用法:
void erase (iterator starting_iterator, iterator ending_iterator);
參數:此方法接受以下參數:
- starting_iterator:它是指要從多重集中刪除的值範圍的起始迭代器。
- ending_iterator:它是指要從多重集中刪除的值範圍的結束迭代器。
返回值:此方法返回最後刪除的元素或結束迭代器之後的迭代器。
以下示例說明了multiset::erase()方法的用法方式:
// C++ program to demonstrate // multiset::erase() method #include <bits/stdc++.h> using namespace std; int main() { // Initialise the multiset multiset<int> multi_set; multiset<int>::iterator ms_iterator; // Add values to the multiset for (int i = 1; i < 10; i++) { multi_set.insert(i); } cout << "Original multiset: "; for (ms_iterator = multi_set.begin(); ms_iterator != multi_set.end(); ++ms_iterator) cout << ' ' << *ms_iterator; cout << '\n'; ms_iterator = multi_set.begin(); ms_iterator++; ms_iterator++; // Passing the iterator range for the positions // at which the values are to be erased auto ir = multi_set.erase(ms_iterator, multi_set.end()); cout << "Modified multiset: "; for (ms_iterator = multi_set.begin(); ms_iterator != multi_set.end(); ++ms_iterator) cout << ' ' << *ms_iterator; cout << '\n'; (ir == multi_set.end()) ? cout << "Return value is: multi_set.end()\n " : cout << "Return value is not multi_set.end()\n"; return 0; }
輸出:Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 2 Return value is: multi_set.end();
相關用法
- C++ set::erase用法及代碼示例
- C++ map erase()用法及代碼示例
- C++ multimap::erase()用法及代碼示例
- C++ unordered_map erase用法及代碼示例
- C++ std::string::erase用法及代碼示例
- C++ unordered_multimap erase用法及代碼示例
- C++ unordered_set erase()用法及代碼示例
- C++ list erase()用法及代碼示例
- C++ unordered_multiset erase()用法及代碼示例
- C++ vector erase()、clear()用法及代碼示例
- C++ multiset max_size()用法及代碼示例
- C++ multiset::operator=用法及代碼示例
注:本文由純淨天空篩選整理自Samdare B大神的英文原創作品 multiset erase() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。