C ++ STL中的set :: empty()函数
C++STLset::empty()函数
set::empty()函数是预定义的函数,用于检查集合是否为空。如果set为空,则返回true(1),如果set不为空,则返回false。
原型:
set<T> st; //声明
set<T>::iterator it; //迭代器声明
st.empty( );参数:无通过
返回类型:布尔型(True或False)
用法:该函数检查集合是否为空。
示例
For a set of integer,
set<int> st;
st.insert(4);
st.insert(5);
set content:
4
5
Bool check=st.empty();
check =False
St.erase(st.begin()); //擦除4-
St.erase(st.begin()); //擦除5-
Set content:
Empty set
//现在再次检查
check=st.empty()
check=TRUE包含的头文件:
#include <iostream>
#include <set>
OR
#include <bits/stdc++.h>C++实现:
#include <bits/stdc++.h>
using namespace std;
void printSet(set<int> st){
set<int>:: iterator it;
cout<<"Set contents are:\n";
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
cout<<"Example of empty function\n";
set<int> st;
set<int>:: iterator it;
cout<<"inserting 4\n";
st.insert(4);
cout<<"inserting 6\n";
st.insert(6);
cout<<"inserting 10\n";
st.insert(10);
printSet(st); //打印当前设置
if(st.empty())
cout<<"It's empty\n";
else
cout<<"It's not empty\n";
cout<<"erasing all elements\n";
st.clear();
if(st.empty())
cout<<"It's empty\n";
else
cout<<"It's not empty\n";
return 0;
}输出结果
Example of empty function inserting 4 inserting 6 inserting 10 Set contents are: 4 6 10 It's not empty erasing all elements It's empty