- multimap :: erase()是C++ STL中的内置函数,用于从容器中删除元素。它可用于擦除键,任何指定位置或给定范围内的元素。
- 删除密钥的语法:
multimap_name.erase(key)
参数:该函数接受一个强制性参数键,该键指定要在多图容器中擦除的键。
返回值:该函数不返回任何内容。它使用指定的键擦除所有元素。
// C++ program to illustrate // multimap::erase(key) #include
using namespace std; int main() { // initialize container multimap mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 2, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "The multimap before using erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.begin(); itr != mp.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // function to erase given keys mp.erase(1); mp.erase(2); // prints the elements cout << "\nThe multimap after applying erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } 输出:The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 5 50 3 60
- 删除头寸的语法:
multimap_name.erase(iterator position)
参数:函数接受一个强制性参数位置,该位置指定迭代器,该迭代器是要删除元素的位置的参考。
返回值:该函数不返回任何内容。
下面的程序说明了上面的语法:
// C++ program to illustrate // multimap::erase(position) #include
using namespace std; int main() { // initialize container multimap mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 2, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "The multimap before using erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.begin(); itr != mp.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // function to erase given position auto it = mp.find(2); mp.erase(it); auto it1 = mp.find(5); mp.erase(it1); // prints the elements cout << "\nThe multimap after applying erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } 输出:The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 3 60 2 20 1 40
- 删除给定范围的语法:
multimap_name.erase(iterator position1, iterator position2)
参数:该函数接受两个强制性参数,如下所述:
- position1 –指定迭代器,该迭代器是要从中进行删除的元素的引用。
- position2 –指定迭代器,该迭代器是要进行删除的元素的引用。
返回值:该函数不返回任何内容。它删除给定迭代器范围内的所有元素。
下面的程序说明了上面的语法:
// C++ program to illustrate // multimap::erase() #include
using namespace std; int main() { // initialize container multimap mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 2, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "The multimap before using erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.begin(); itr != mp.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // function to erase in a given range // find() returns the iterator reference to // the position where the element is auto it1 = mp.find(2); auto it2 = mp.find(5); mp.erase(it1, it2); // prints the elements cout << "\nThe multimap after applying erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } 输出:The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 5 50 1 40
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。