set / multiset容器

183 阅读2分钟

set基本概念

简介:

  • 所有元素都会在插入时自动被排序

本质:

  • set/multiset属于关联式容器,底层结构是傭二叉树实现

set和multiset区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

set容器构造和赋值

函数原型:

构造:

  • set st; //默认构造函数
  • set(const set& st); //拷贝构造函数

赋值:

  • set& operator=(const set& st); //重载等号操作符

set容器构造和赋值案例:

void PrintSet(set& s) {

for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
	cout << *it << " ";
}
cout << endl;

}

void test01() {

set<int>st;

//插入数据 只有insert方式
st.insert(10);
st.insert(30);
st.insert(40);
st.insert(60);
st.insert(60);

//遍历容器
//set容器特点:  所有元素插入时候自动被排序
//set容器不允许插入重复值
//10 30 40 60
PrintSet(st);

//拷贝构造
set<int>st2(st);
PrintSet(st2);

//operator=赋值
set<int>st3;
st3 = st2;
PrintSet(st3);

}

set容器大小和交换

函数原型:

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

set容器大小和交换案例:

void PrintSet(set& s) {

for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
	cout << *it << " ";
}
cout << endl;

}

//大小 void test01() {

//创建set容器
set<int>st;

//插入数据
st.insert(10);
st.insert(30);
st.insert(50);
st.insert(20);
st.insert(40);

//打印容器
PrintSet(st);

//判断是否为空
if (st.empty())
{
	cout << "st为空!" << endl;
}
else
{
	cout << "st不为空!" << endl;
	cout << "st的大小为:" << st.size() << endl;
}

}

//交换 void test02() {

//创建set容器
set<int>st;

//插入数据
st.insert(10);
st.insert(30);
st.insert(50);
st.insert(20);
st.insert(40);

set<int>st2;
st2.insert(100);
st2.insert(200);
st2.insert(300);

cout << "交换前:" << endl;
PrintSet(st);
PrintSet(st2);

cout << "交换后:" << endl;
st.swap(st2);
PrintSet(st);
PrintSet(st2);

}

set容器插入和删除

函数原型:

  • insert(elem); //在容器中插入元素
  • clear(); //清楚所有元素
  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
  • erase(beg , end); //删除区间[beg , edn]的所有元素,返回下一个元素的迭代器
  • erase(elem); //删除容器中值为elem的元素

set容器插入和删除案例:

void PrintSet(set& s) {

for (set<int>::iterator it = s.begin(); it != s.end(); it ++ )
{
	cout << *it << " ";
}
cout << endl;

}

void test01() {

set<int>s;

//插入
s.insert(10);
s.insert(30);
s.insert(20);
s.insert(40);

//遍历
PrintSet(s);

//删除
s.erase(s.begin());
PrintSet(s);

//删除重载版本
s.erase(30);
PrintSet(s);

//清空
//s.erase(s.begin(), s.end());
s.clear();
PrintSet(s);

}

set容器查找和统计

函数原型:

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • count(key); //统计key的元素个数

set容器查找和统计案例:

void test01() {

//创建set容器
set<int>s;
//插入数据
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);

//查找
set<int>::iterator pos = s.find(30);

if (pos != s.end())
{
	cout << "找到元素:" << *pos << endl;
}
else
{
	cout << "未能找到元素!" << endl;

}

}

//统计 void test02() {

//创建set容器
set<int>s;
//插入数据
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(30);

//统计30的个数
int num = s.count(30); 
//对于set而言  统计的结果 要么是0  要么是1  set不支持插入重复元素
cout << "num = " << num << endl;

}

set和multiset区别

区别:

  • set不可以插入重复数据,而multiset可以
  • set插入数据的同时会返回插入结果,标识插入是否成功
  • multiset不会检测数据,因此可以插入重复数据

set和multiset区别案例:

void test01() {

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>mu;
//允许插入重复值
mu.insert(20);
mu.insert(20);

for (multiset<int>::iterator it = mu.begin(); it != mu.end(); it++)
{
	cout << *it << " ";
}
cout << endl;

}

pair对组创建

函数原型:

  • pair<type , type> p (value1 , value2);
  • pair<type , type> p = make_pair(value1 , value2);

pair对组创建案例:

void test01() {

//第一种方式
pair<string, int> p(string("Tom") , 20);

cout << "姓名:" << p.first << " 年龄:" << p.second << endl;

//第二种方式
pair<string, int>p2 = make_pair("Jerry", 30);

cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;

}

set容器排序

主要技术点:

  • 利用仿函数,可以改变排序规则

set容器排序案例:

class MyCompare {

public:

bool operator()(int v1 , int v2) const
{
	return v1 > v2;
}

};

void test01() {

set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(50);

for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
	cout << *it << " ";
}
cout << endl;

//指定排序规则从大到小 利用仿函数
set<int,MyCompare>s2;
s2.insert(10);
s2.insert(40);
s2.insert(30);
s2.insert(20);
s2.insert(50);

for (set<int,MyCompare>::const_iterator it = s2.begin(); it != s2.end(); it++)
{
	cout << *it << " ";
}
cout << endl;

}