C ++ STL中的set :: clear()函数
C++STLset::clear()函数
set::clear()函数是一个预定义的函数,它用于清除整个集合,无论其元素如何。
原型:
set<T> st; //声明
st.clear()参数:无通过
返回类型:无
用法:该函数清除整个集合,而不考虑其元素。
示例
For a set of integer,
set<int> st;
st.insert(4);
st.insert(5);
set content:
4
5
st.clear();
set content:
empty set包含的头文件:
#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";
if(st.empty()){
cout<<"empty set\n";
return;
}
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
cout<<"Example of clear 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); //打印当前设置
cout<<"clearing all elements\n";
st.clear();
printSet(st);
return 0;
}输出结果
Example of clear function inserting 4 inserting 6 inserting 10 Set contents are: 4 6 10 clearing all elements Set contents are: empty set
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短