C++ STL进阶与补充(set/multiset容器)

集合容器:所有元素在插入时自动排序,set/multiset属于关联式容器,底层结构由二叉树实现。

Set和multiset的区别:set不允许容器中有重复的元素;multiset允许容器中有重复的元素。

Set在插入数据时会返回插入结果,表示插入是否成功。Multiset则不会检测。

例:

#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方式
	pair<set<int>::iterator, bool> ret = s1.insert(10);  //set是一个对组的数据类型
	if (ret.second)
	{
		cout << "set第1次插入成功!" << endl;
	}
	else
	{
		cout << "set第1次插入失败!" << endl;
	}
	ret=s1.insert(10);
	if (ret.second)
	{
		cout << "set第2次插入成功!" << endl;
	}
	else
	{
		cout << "set第2次插入失败!" << endl;
	}
	multiset<int> s2;
	s2.insert(10);
	s2.insert(10);
	s2.insert(10);
	for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		//multiset不会检测,直接打印即可
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
}

1、set构造和赋值

Set<T> st;

默认构造

Set(const set& st)

拷贝构造

=

等号运算符重载,等号赋值

Set在插入数据时,只有Insert的方式。

Set容器的特点:1、所有元素插入时自动排序;2、不允许插入重复的值

#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;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(20); //不报错,但是并不会插入
	s1.insert(40);
	printSet(s1);
}
int main()
{
	test01();
}

2、set大小和互换

Size()

返回容器中元素个数。

Empty()

判断容器是否为空。

Swap(st)

交换两个集合的容器。

Set容易不允许有修改大小的操作,例如resize。

#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;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	printSet(s1);
	if (s1.empty())
	{
		cout << "s1为空。" << endl;
	}
	else
	{
		cout << "s1不为空。" << endl;
		cout << "s1的大小为:" << s1.size() << endl;
	}
}
int main()
{
	test01();
}

3、set插入和删除

Insert(elem)

在容器中插入元素

Clear()

清空所有元素

Erase(pos)

删除pos迭代器所指的元素,返回下一个元素的地址的迭代器

Erase(beg,end)

删除区间[beg,end]的所有元素,返回下一个元素的迭代器

Erase(elem)

删除容器中值为elem的元素

#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(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	printSet(s1); //10 20 30
	//删除
	s1.erase(s1.begin());
	printSet(s1); //20 30 40
	//删除的重载版本
	s1.erase(30);
	printSet(s1); //20 40
}
int main()
{
	test01();
}

4、set查找和统计

Find(key)

查找key元素是否存在,若存在则返回键的元素的迭代器,否则返回sed.end()

Count(key)

统计key的元素个数,通常来说只返回0和1,multiset则可能大于1.

#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(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set<int>::iterator pos = s1.find(30);
	if (pos != s1.end())
	{
		cout << "找到元素:" << *pos << endl;
	}
	else
	{
		cout << "未找到!" << endl;
	}
}
int main()
{
	test01();
}

5、pair对组的创建

对组:成对出现的数据,可以利用对组返回两个数据。

Pair<type, type> p(value, value);

默认构造

Pair<type, type> p=make_pair(value, value);

Make_pair方式

#include<iostream>
#include"string"
using namespace std;
void test01()
{
	pair<string, int> p("Tom", 20);
	cout << "Name: " << p.first << " " << "Age: " << p.second << endl;
	//第二种方式
	pair<string, int> p2 = make_pair("Jerry", 30);
	cout << "Name: " << p2.first << " " << "Age: " << p2.second << endl;
}
int main()
{
	test01();
}

6、set内置类型指定排序规则

Set容器默认排序规则为从小到大,那么如何改变其排序规则呢?主要利用仿函数。

可以分为两种情况的排序:首先是内置数据类型的排序,如int,char等,其次是自定义数据类型的排序。

case 1:

#include<iostream>
#include<set>
using namespace std;
class myCompare
{
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;//降序排列
	}
};
void test01()
{
	set<int> s1;
	//需要在插入数据之前,告诉set排序方式是什么
	s1.insert(30);
	s1.insert(10);
	s1.insert(50);
	s1.insert(40);
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	set<int,myCompare> s2; //需要在插入数据之前,告诉set排序方式是什么
	s2.insert(30);
	s2.insert(10);
	s2.insert(50);
	s2.insert(40);
	for (set<int,myCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
}

case 2:

#include<iostream>
#include<set>
#include"string"
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 myCompare
{
public:
	bool operator()(const Person& p1, const Person& p2) const
	{
		return p1.m_age >p2.m_age ;//降序排列
	}
};
void test01()
{
	set<Person,myCompare> s;
	Person p1("WXQ", 28);
	Person p2("ZWY", 22);
	Person p3("WAL", 30);
	Person p4("ZYM", 27);
	//需要在插入数据之前,告诉set排序方式是什么,否则会报错
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	for (set<Person,myCompare>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "name: " << (*it).m_name<<" "<<"age: "<<(*it).m_age<<endl;
	}
}
int main()
{
	test01();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值