先决条件:多集
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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。