当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ set clear()用法及代码示例


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

用法

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

参数

返回值

复杂度

大小呈线性。

迭代器有效性

与此容器相关的所有迭代器、引用和指针都将失效。

数据竞争

容器被修改。

所有包含的元素都被修改。

异常安全

此函数从不抛出异常。

例子1

让我们看一个简单的例子来计算清除操作前后集合的大小:

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

int main() {

   set<int> 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 <iostream>
#include <set>

using namespace std;

int main ()
{
  set<string> myset;

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

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

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

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

  return 0;
}

输出:

myset contains:
Aman
Deep
Nikita

myset contains:
Divya
Raaz

在上面的例子中,清除集合后,我们可以在不初始化的情况下添加新元素。

例子3

让我们看一个简单的例子来清除集合的元素:

#include <iostream>
#include <set>

using namespace std;

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

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

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

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

using namespace std;

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

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






相关用法


注:本文由纯净天空筛选整理自 C++ set clear()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。