大小和交换
函数原型:
代码语言:javascript复制#include<iostream>
using namespace std;
#include<set>
void p(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it )
{
cout << *it << " ";
}
cout << endl;
}
void test()
{
set<int> s1 = {1,2,3};
//插入数据,只有用insert方式
s1.insert(4);
s1.insert(6);
s1.insert(6);
s1.insert(5);
//对于set容器而言重复元素插入会不成功,但不会报错
if (s1.empty())
{
cout << "容器为空" << endl;
}
else {
cout << "容器大小" << s1.size() << endl;
}
set<int> s2 = { 8,9,5 };
cout << "交换前: " << endl;
cout << "s2: ";
p(s2);
s2.swap(s1);
cout << "交换后: " << endl;
cout << "s2: ";
p(s2);
}
int main()
{
test();
system("pause");
return 0;
}