當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ map clear()用法及代碼示例


C++ map clear() 函數用於移除Map容器的所有元素。它清除Map並將其大小設置為 0。

用法

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

參數

返回值

例子1

讓我們看一個簡單的例子來計算清除操作前後Map的大小。

#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

在上麵的例子中,一個Map被初始化為 5 個元素。因此,Map的大小為5,但經過清除操作後,大小變為0。

例子2

讓我們看一個簡單的例子來清除Map的元素。

#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 

在上麵的例子中,清除Map後,我們可以在不初始化的情況下添加新元素。

例子3

讓我們看一個簡單的例子來清除Map的元素。

#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.

在上麵的例子中,一個Map有三組,根據用戶的選擇,已經刪除了一組。

示例 4

讓我們看一個簡單的例子:

#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

在上麵的例子中,一個水果Map被初始化為三個水果。要求清圖,如果你輸入0則水果桶有3個元素,如果你輸入1則它會清除水果Map並且大小變為0。




相關用法


注:本文由純淨天空篩選整理自 C++ map clear() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。