C++ Map 中的 clear函数

首页 / C++入门教程 / C++ Map 中的 clear函数

C++ map clear()函数用于删除映射集合的所有元素。它将清除映射并将其大小设置为0。

clear - 语法

void clear(); //until C++ 11
void clear() noexcept; //since C++ 11

clear - 例子1

让我们看一个简单的示例,在清除操作之前和之后计算映射的大小。

无涯教程网

#include <iostream>
#include <map>

using namespace std;

int main() {

   map<char, int> mymap = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Initial size of map before clear operation = " << mymap.size() << endl;

   mymap.clear();

   cout << "Size of map after clear opearation = " << mymap.size() << endl;

   return 0;
}

输出:

Initial size of map before clear operation = 5
Size of map after clear opearation = 0

在上面的示例中,映射是使用5个元素初始化的。因此,映射的大小为5,但清除操作后,大小变为0。

clear - 例子2

让我们看一个简单的示例,以清除映射上的元素。

#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<int,string> mymap;

  mymap[1] = "Nikita";
  mymap[2] = "Deep";
  mymap[3] = "Ashish";

  cout << "mymap contains:\n";
  for (map<int,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  mymap.clear();
  
  mymap[4] = "Rajni";
  mymap[5] = "Sunil";

  cout << "\nmymap contains:\n";
  for (map<int,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  return 0;
}

输出:

mymap contains:
1 : Nikita
2 : Deep
3 : Ashish

mymap contains:
4 : Rajni
5 : Sunil 

在上面的示例中,清除映射后,我们可以添加新元素而无需初始化。

clear - 例子3

让我们看一个简单的示例,以清除映射上的元素。

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  int n;
  map<int,string> m1,m2,m3;

  m1[1] = "Nikita";
  m1[2] = "Deep";
  m1[3] = "Ashish";
  
  m2[1] = "Nidhi";
  m2[2] = "Priya";
  m2[3] = "Gitanjali";
   m3[1] = "Rakesh";
  m3[2] = "Fruti";
  m3[3] = "Kamlesh";
  cout << "m1 group has following members:\n";
  for (map<int,string>::iterator it=m1.begin(); it!=m1.end(); ++it)
  cout << it->first << " : " << it->second << '\n';
  cout << "m2 group has following members:\n";
  for (map<int,string>::iterator it=m2.begin(); it!=m2.end(); ++it)
  cout << it->first << " : " << it->second << '\n';
  cout << "m3 group has following members:\n";
  for (map<int,string>::iterator it=m3.begin(); it!=m3.end(); ++it)
   cout << it->first << " : " << it->second << '\n';
   cout<<"\nWhich group do you want to delete?\n 1.m1\n 2.m2\n 3.m3\n Please enter your choice: ";
  cin>>n;
  if(n==1){
  m1.clear();
  cout<<"\nGroup m1 has been cleared.";
  }
  else if(n==2){
  m2.clear();
  cout<<"\nGroup m2 has been cleared.";
  }
  else if(n==3){
  m3.clear();
  cout<<"\nGroup m3 has been cleared.";
  }
  else
  cout<<"Invalid option!";
  
  return 0;
}

输出:

m1 group has following members:
1 : Nikita
2 : Deep
3 : Ashish
m2 group has following members:
1 : Nidhi
2 : Priya
3 : Gitanjali
m3 group has following members:
1 : Rakesh
2 : Fruti
3 : Kamlesh

Which group do you want to delete?
 1. m1
 2. m2
 3. m3
 Please enter your choice: 2

Group m2 has been cleared.

在上面的示例中,映射分为三组,根据用户的选择,一组已被删除。

clear - 例子4

让我们看一个简单的例子:

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-map-clear-function.html

来源:LearnFk无涯教程网

#include <iostream>
#include <map>
 using namespace std;
 int main() {
 int n;
 map<string, int> fruit = {
            {"Banana", 40},
            {"Apple", 190},
            {"Orange", 120},
            };
 cout << "Fruit bucket has following fruits = \n";
 for (map<string,int>::iterator it=fruit.begin(); it!=fruit.end(); ++it)
 cout << it->first << " : " << it->second << '\n';
cout<<"\nDo you want to clear your fruit bucket?\nPress 1 for Yes and 0 for No: ";
   cin>>n;
   if( n==1){
   fruit.clear();
   cout<<fruit.size()<<" fruits in bucket \n";  
   }
   else if(n==0)
   cout <<fruit.size() << " fruits in bucket \n" ;
    
 return 0;
}

输出:

1. 
Fruit bucket has following fruits = 
Apple : 190
Banana : 40
Orange : 120

Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 0
3 fruits in bucket



2. 
Fruit bucket has following fruits = 
Apple : 190
Banana : 40
Orange : 120

Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 1
0 fruits in bucket

在上面的示例中,一个水果图用三个水果初始化。要求清除映射,如果输入0,则返回水果个数;如果输入1,则它将清除水果图,大小变为0。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Java性能调优实战 -〔刘超〕

TypeScript开发实战 -〔梁宵〕

SRE实战手册 -〔赵成〕

A/B测试从0到1 -〔张博伟〕

程序员的测试课 -〔郑晔〕

全链路压测实战30讲 -〔高楼〕

人人都用得上的数字化思维课 -〔付晓岩〕

快手 · 音视频技术入门课 -〔刘歧〕

Dubbo源码剖析与实战 -〔何辉〕

好记忆不如烂笔头。留下您的足迹吧 :)