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

📅  最后修改于: 2020-10-20 08:01:18             🧑  作者: Mango

C++ STL Set.clear()

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

句法

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

参数

没有

返回值

没有

复杂度

大小线性。

迭代器有效性

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

数据竞争

容器已修改。

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

异常安全

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

例子1

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

#include 
#include 
using namespace std;

int main() {

   set myset = {10,20,30,40}; 

   cout << "Initial size of set before clear operation = " << myset.size() << endl;

   myset.clear();

   cout << "Size of set after clear operation = " << myset.size() << endl;

   return 0;
}

输出:

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

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

例子2

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

#include 
#include 

using namespace std;

int main ()
{
  set myset;

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

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

  myset.clear();
  
  myset= {"Divya", "Raaz"};

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

  return 0;
}

输出:

myset contains:
Aman
Deep
Nikita

myset contains:
Divya
Raaz

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

例子3

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

#include 
#include 

using namespace std;

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

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

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

  cout << "\n\nm3 group has following members:\n";
  for (set::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.

在上面的示例中,有3组Set.,并且根据用户的选择删除了一组。

例子4

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

#include 
#include 

using namespace std;

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

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

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

在上面的示例中,一个水果集用三个水果初始化。要求清除集,如果输入0则水果桶有3个元素,或者如果输入1则将清除水果集,大小变为0。