- 多重映射::擦除() 是C++中的一个内置函数,用于从容器中删除元素。它可以用来擦除任何指定位置或给定范围内的键、元素。
- 删除密钥的语法:
multimap_name.erase(key)
参数: 该函数接受一个强制参数 钥匙 指定要在多重贴图容器中擦除的关键点。
返回值: 该函数不返回任何内容。它用指定的键擦除所有元素。
// C++ program to illustrate
// multimap::erase(key)
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// initialize container
multimap<
int
,
int
> 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 : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
// function to erase given keys
mp.erase(1);
mp.erase(2);
// prints the elements
cout <<
"The multimap after applying erase() is : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.crbegin(); itr != mp.crend(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
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 <bits/stdc++.h>
using
namespace
std;
int
main()
{
// initialize container
multimap<
int
,
int
> 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 : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
// 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 <<
"The multimap after applying erase() is : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.crbegin(); itr != mp.crend(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
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–指定迭代器,该迭代器是对要从中删除的元素的引用。
- 位置2–指定迭代器,该迭代器是对要删除的元素的引用。
返回值: 该函数不返回任何内容。它删除给定迭代器范围内的所有元素。
下面的程序演示了上述语法:
// C++ program to illustrate
// multimap::erase()
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// initialize container
multimap<
int
,
int
> 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 : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
// 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 <<
"The multimap after applying erase() is : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.crbegin(); itr != mp.crend(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
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
null
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END