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


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。