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