📜  C++ STL-multimap.clear()函数

📅  最后修改于: 2020-10-20 05:59:46             🧑  作者: Mango

C++ STL Multimap.clear()函数

C++ multimap clear()函数用于删除multimap容器的所有元素。清除Multimap并将其大小设置为0。

句法

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

参数

没有

返回值

没有

复杂度

大小线性。

迭代器有效性

与该容器相关的所有迭代器,引用和指针均无效。

数据竞争

容器已修改。

所有包含的元素均已修改。

异常安全

此函数永远不会引发异常。

例子1

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

#include 
#include 

using namespace std;

int main() {

   multimap mymultimap = {
            {'a', 1},
            {'b', 2},
            {'b', 3},
            {'d', 4},
            {'d', 5},
            };

   cout << "Initial size of multimap before clear operation = " << mymultimap.size() << endl;

   mymultimap.clear();

   cout << "Size of multimap after clear operation = " << mymultimap.size() << endl;

   return 0;
}

输出:

Initial size of multimap before clear operation = 5
Size of multimap after clear operation = 0

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

例子2

让我们看一个简单的示例,以清除Multimap的元素:

#include 
#include 

using namespace std;

int main ()
{
  multimap mymultimap;

  mymultimap = {
               {1, "Nikita"},
               {2, "Divya" },
               {1, "Ashish"} };

  cout << "mymultimap contains:\n";
  for (multimap::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  mymultimap.clear();
  
  mymultimap = {
               {1, "Aman"},
               {2, "Deep"}
               };
               
  cout << "\nmymultimap contains:\n";
  for (multimap::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  return 0;
}

输出:

mymultimap contains:
1 : Nikita
1 : Ashish
2 : Divya

mymultimap contains:
1 : Aman
2 : Deep

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

例子3

让我们看一个简单的示例,以清除Multimap的元素:

#include 
#include 

using namespace std;

int main ()
{
  int n;
  multimap m1,m2,m3;

  m1 = { {1, "Nikita"},
         {2, "Deep"},
         {1, "Ashish"}  };
  
  m2 = { {1, "Nidhi"},
        {2, "Priya"},
        {2, "Gitanjali"} };
  
  m3 = { {1, "Manas"},
       {3, "Fruti"},
       {3, "Kamlesh"} };
  

  cout << "m1 group has following members:\n";
  for (multimap::iterator it=m1.begin(); it!=m1.end(); ++it)
    cout << it->first << " : " << it->second << '\n';
  

  cout << "m2 group has following members:\n";
  for (multimap::iterator it=m2.begin(); it!=m2.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  cout << "m3 group has following members:\n";
  for (multimap::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
1 : Ashish
2 : Deep

m2 group has following members:
1 : Nidhi
2 : Priya
2 : Gitanjali

m3 group has following members:
1 : Manas
3 : 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.

在上面的示例中,有三组Multimap,并且根据用户的选择,已删除了一组。

例子4

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

#include 
#include 

using namespace std;

int main() {
    
   int n;
   
   multimap fruit = {
            {"Banana", 40},
            {"Apple", 190},
            {"Orange", 120},
            };

   cout << "Fruit bucket has following fruits = \n";
   for (multimap::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<

输出:

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则要求清除Multimap,则水果桶有3个元素;如果输入1,则将清除水果Multimap,大小变为0。