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。