- 所有元素会在插入时自动排序
- 底层结构是二叉树
- set和multiset区别.set不允许重复,multiset可以重复
- bool compare()(int int) const 要加const
- test08对于自定义数据类型,set必须指定排序规则才可以插入数据
#include<iostream>
#include<set>
#include<string>
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);
set<int>s2(s1);
printset(s2);
set<int>s3;
s3 = s1;
printset(s3);
}
void test02()
{
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;
}
set<int> s2;
s2.insert(100);
s2.insert(200);
s2.insert(300);
s2.insert(400);
printset(s2);
cout << "交换前: " << endl;
printset(s1);
printset(s2);
s1.swap(s2);
cout << "交换后: " << endl;
printset(s1);
printset(s2);
}
void test03()
{
set<int> s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
printset(s1);
s1.erase(s1.begin());
printset(s1);
set<int>::iterator it = s1.begin();
s1.erase(it);
printset(s1);
s1.erase(30);
printset(s1);
s1.clear();
printset(s1);
}
void test04()
{
set<int> s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
printset(s1);
set<int>::iterator pos = s1.find(300);
if (pos != s1.end()) {
cout << "找到了元素:" << *pos << endl;
}
else
{
cout << "找不到" << endl;
}
int num = s1.count(300);
cout << "num = " << num << endl;
}
void test05()
{
set<int> s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second) {
cout << "第一次插入成功!" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "第二次插入成功" << endl;
}
else
{
cout << "第二次插入失败" << endl;
}
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test06()
{
set<int> s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
printset(s1);
pair<string, int> p(string("Tom"), 20);
cout << "姓名: " << p.first << " 年龄: " << p.second;
pair<string, int> p2 = make_pair("Tom", 20);
cout << "姓名: " << p.first << " 年龄: " << p.second;
}
class Mycompare
{
public:
bool operator()(int v1, int v2) const
{
return v1 > v2;
}
};
void test07()
{
set<int> s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
printset(s1);
for (set<int>::const_iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
set<int, Mycompare> s2;
s2.insert(10);
s2.insert(20);
s2.insert(30);
s2.insert(40);
s2.insert(30);
for (set<int,Mycompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
class Person
{
public:
Person(string name, int age)
{
mname = name;
mage = age;
}
string mname;
int mage;
};
class compareperson
{
public:
bool operator()(const Person& p1, const Person& p2) const
{
return p1.mage > p2.mage;
}
};
void test08()
{
set<Person, compareperson> s;
Person p1("刘备", 24);
Person p2("关羽", 26);
Person p3("张飞", 27);
Person p4("赵云", 23);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, compareperson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名:" << it->mname << "年龄:" << it->mage << endl;
}
}
int main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
return 0;
}