- set/multiset容器
基本概念:所有元素都会在插入时自动排序。
本质:set/multiset容器属于关联式容器,底层结构是用二叉树实现的。
区别:set不允许容器中有重复的元素;multiset允许有重复的元素。
- set构造和赋值
#include <iostream>
#include <set>
using namespace std;
void printSet(const set<int> &s)
{
for(set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//默认构造
set<int> s1;
//插入数据,只有insert方式
s1.insert(30);
s1.insert(20);
s1.insert(10);
s1.insert(40);
s1.insert(40);//无效,容器里只有一个40
//遍历容器,set容器自动排序
printSet(s1);
//拷贝构造
set<int> s2(s1);
printSet(s2);
//赋值操作
set<int> s3;
s3 = s1;
printSet(s3);
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
set大小和交换:set容器没有resize接口,因为他允许有重复的元素,如果重置大小,则默认用0补齐是不成立的。
#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;
set<int> s2;
//插入数据,只有insert方式
s1.insert(30);
s1.insert(20);
s1.insert(10);
s2.insert(1);
s2.insert(3);
s2.insert(2);
//判断是否为空
if(s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
}
cout << "s1的大小为:" << s1.size() << endl;
cout << "交换前s1为:";
printSet(s1);
cout << "交换后s2为:";
printSet(s2);
//交换
s1.swap(s2);
cout << "交换后s1为:";
printSet(s1);
cout << "交换后s2为:";
printSet(s2);
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
set插入和删除
#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;
//插入数据,只有insert方式
s1.insert(30);
s1.insert(20);
s1.insert(10);
s1.insert(40);
printSet(s1);
//删除
s1.erase(s1.begin());//删除自动排序后的第一个元素
printSet(s1);
//删除重载版本
s1.erase(30);//删除30这个元素
printSet(s1);
//清空
s1.erase(s1.begin(), s1.end());
s1.clear();
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
set查找和统计:
#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;
//插入数据,只有insert方式
s1.insert(30);
s1.insert(20);
s1.insert(10);
s1.insert(40);
//查找(返回找到的位置)
set<int>::iterator pos = s1.find(30);
if(pos != s1.end())
{
cout << "找到元素" << *pos << endl;
}
else
{
cout << "未找到该元素" << endl;
}
//统计30的个数(对于set而言,统计结果要不为0要不为1)
int num = s1.count(30);
cout << "num = " << num << endl;
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
set和multiset的区别:
#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> s;
//pair为对组
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;
}
}
void test02()
{
multiset<int> ms;
//允许重复插入,不会做检测
ms.insert(10);
ms.insert(10);
for(multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(int argc, char const *argv[])
{
test01();
test02();
return 0;
}
pair对组即成对出现的数据,利用对组可以返回两个数据
#include <iostream>
#include <string>
using namespace std;
//pair对组的创建
void test01()
{
//第一种方式
pair<string, int> p1("Tom" , 20);
cout << "姓名:" << p1.first << "年龄:" << p1.second << endl;
//第二种方式
pair<string, int> p2 = make_pair("Jerry", 30);
cout << "姓名:" << p2.first << "年龄:" << p2.second << endl;
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
set容器排序:主要利用仿函数,改变排序规则
#include <iostream>
#include <set>
using namespace std;
class myCompare
{
public:
bool operator()(int v1, int v2)
{
return v1> v2;
}
};
void printSet(const set<int> &s)
{
for(set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int> s1;
s1.insert(30);
s1.insert(20);
s1.insert(10);
s1.insert(40);
//默认排序从小到大
printSet(s1);
//指定排序规则为从大到小
set<int, myCompare> s2;
s2.insert(30);
s2.insert(20);
s2.insert(10);
s2.insert(40);
//printSet(s2);//不能直接调用打印函数(容器参数不对)
for(set<int, myCompare>::const_iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
set容器排序,存放自定义类型
#include <iostream>
#include <set>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class comparePerson
{
public:
bool operator()(const Person &p1, const Person &p2)
{
//按照年龄降序
return p1.m_age > p2.m_age;
}
};
void printSet(const set<Person, comparePerson> &s)
{
for(set<Person, comparePerson>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << (*it).m_name << " " << (*it).m_age;
cout << endl;
}
}
void test01()
{
//自定义数据类型都会指定排序规则
set<Person, comparePerson> s1;
Person p1("刘备" , 37);
Person p2("曹操" , 23);
Person p3("关于" , 53);
Person p4("张飞" , 57);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
printSet(s1);
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}