先决条件:多套
multiset :: erase()是C++中的STL函数,它从多集中删除指定的元素。
此方法有三个版本。这些都是:
- 句法:
void erase (iterator position_of_iterator);
参数:此方法接受以下参数:
- position_of_iterator:它是指要在迭代器的帮助下删除的特定元素的位置。
返回值:该方法返回被删除元素之后的迭代器。
下面的示例说明multiset :: erase()方法的工作方式:
// C++ program to demonstrate // multiset::erase() method #include
using namespace std; int main() { // Initialise the multiset multiset multi_set; multiset ::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
using namespace std; int main() { // Initialise the multiset multiset multi_set; multiset ::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
using namespace std; int main() { // Initialise the multiset multiset multi_set; multiset ::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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。