STLset容器大小和交换

STLset容器大小和交换

功能描述:
统计set容器大小以及交换set容器
函数原型:

size();        //返回容器中元素的数目
empty();      //判断容器是否为空
swap(st);     //交换两个集合容器

代码示例:

#include<iostream>
#include<set>
using namespace std;
void printSet(set<int>&s)
{
       for (set<int>::iterator it = s.begin(); it != s.end(); it++)
       {
              cout << *it << " ";
       }
       cout << endl;
}
void test01()
{
       set<int>s1;
       //插入数据
       s1.insert(10);
       s1.insert(20);
       s1.insert(30);
       s1.insert(40);
       //打印容器
       printSet(s1);
       //判断是否为空
       if (s1.empty())
       {
              cout << "s1为空" << endl;
       }
       else
       {
              cout << "s1不为空" << endl;
              cout << "s1的大小为:" << s1.size() << endl;
       }
}
void test02()
{
       set<int>s1;
       //插入数据
       s1.insert(10);
       s1.insert(20);
       s1.insert(30);
       s1.insert(40);
       set<int>s2;
       //插入数据
       s2.insert(100);
       s2.insert(200);
       s2.insert(300);
       s2.insert(400);
       cout << "交换前:" << endl;
       printSet(s1);
       printSet(s2);
       cout << "交换后:" << endl;
       s1.swap(s2);
       printSet(s1);
       printSet(s2);
}
int main()
{
       test01();
       test02();
       return 0;
}

总结:

统计大小—size
判断是否为空—empty
交换容器—swap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑马金牌编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值