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

📅  最后修改于: 2020-10-19 07:50:11             🧑  作者: Mango

C++多重清除()

C++ Multimultiset clear()函数用于删除多集容器的所有元素。清除多集并将其大小转换为0。

句法

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

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

参数

没有

返回值

没有

复杂度

大小线性。

迭代器有效性

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

数据竞争

容器已修改。

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

异常安全

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

例子1

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

#include 
#include 
using namespace std;

int main() {

   multiset mymultiset = {10,20,20,30}; 

   cout << "Initial size of multiset before clear operation = " << mymultiset.size() << endl;

   mymultiset.clear();

   cout << "Size of multiset after clear operation = " << mymultiset.size() << endl;

   return 0;
}

输出:

Initial size of multiset before clear operation = 4
Size of multiset after clear operation = 0

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

例子2

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

#include 
#include 

using namespace std;

int main ()
{
  multiset mymultiset;

  mymultiset = {"Nikita","Deep","Aman"};

  cout << "mymultiset contains:\n";
  for (multiset::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';

  mymultiset.clear();
  
  mymultiset= {"Deep", "Raaz"};

  cout << "\nmymultiset contains:\n";
  for (multiset::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';

  return 0;
}

输出:

mymultiset contains:
Aman
Deep
Nikita

mymultiset contains:
Deep
Raaz

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

例子3

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

#include 
#include 

using namespace std;

int main ()
{
  int n;
  multiset m1,m2,m3;
  
  m1 = {"Hello", "World"};
  m2 = {"Java", "Program"};
  m3 = {"C++", "Coding"};

  cout << "m1 group has following members:\n";
  for (multiset::iterator it=m1.begin(); it!=m1.end(); ++it)
    cout << *it << ' ';
  

  cout << "\n\nm2 group has following members:\n";
  for (multiset::iterator it=m2.begin(); it!=m2.end(); ++it)
    cout << *it<< ' ';

  cout << "\n\nm3 group has following members:\n";
  for (multiset::iterator it=m3.begin(); it!=m3.end(); ++it)
    cout << *it<< ' ';
  
  cout<<"\n\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:
Hello World 

m2 group has following members:
Java Program 

m3 group has following members:
C++ Coding 

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

Group m2 has been cleared.

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

例子4

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

#include 
#include 

using namespace std;

int main() {
    
   int n;
   
   multiset fruit = {"Banana","Apple","Orange","Banana"};

   cout << "Fruit bucket has following fruits = \n";
   for (multiset::iterator it=fruit.begin(); it!=fruit.end(); ++it)
    cout << *it<< '\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
Banana
Banana
Orange

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


2. 
Fruit bucket has following fruits = 
Apple 
Banana 
Banana
Orange

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

在上面的示例中,一个水果多重集用三个水果初始化。如果您输入0,则要求清除多重集,然后水果桶有4个元素;如果输入1,则将清除水果多重集,大小变为0。