初识STL之set容器

1.set基本概念

简介:
所有的元素都会在插入时自动排序

本质:
set/multiset属于关联式容器,底层结构式用二叉树实现的.

set和multiset区别:

set不允许容器里面有重复的元素
multiset允许容器里面有重复的元素

2.set构造和赋值

构造:

set<T> st;
set<const set& st>;

赋值:

set& operator=(const set& st);
#include<iostream>
#include<set>
using namespace std;

void PrintSet(set<int>& s)
{
	for(auto s1:s)
		cout<<s1<<" ";
	cout<<endl;
}

void text()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(30);
	s1.insert(30);
	s1.insert(20);
	//10 20 30 40
	PrintSet(s1);
	
	set<int> s2(s1);
	//10 20 30 40
	PrintSet(s2);
	
	set<int> s3;
	s3 = s1;
	//10 20 30 40
	PrintSet(s3);
	
}


int main()
{
	text();
}

3.set大小和交换

函数原型:

size();
empty();
swap(st);
#include<iostream>
#include<set>
using namespace std;

void PrintSet(set<int>& s)
{
	for(auto s1:s)
		cout<<s1<<" ";
	cout<<endl;
}

void text()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	
	int Size = s1.size();
	cout<<Size<<endl;
	cout<<"交换前"<<endl; 
	if(!s1.empty()) PrintSet(s1);
	
	set<int> s2;
	s2.insert(100);
	s2.insert(400);
	s2.insert(300);
	s2.insert(200);
	PrintSet(s2);
	
	cout<<"交换后"<<endl; 
	s1.swap(s2);
	PrintSet(s1);
	PrintSet(s2); 
	
}


int main()
{
	text();
}

4.set的插入和删除

函数原型:

insert(elem);
clear();
erase(pos);  //删除pos迭代器所指向的元素,返回下一个元素的迭代器
erase(beg,end); //删除区间[beg,end)所有的元素,返回下一个元素的迭代器
erase(elem);//删除容器中elem元素;
#include<iostream>
#include<set>
using namespace std;

void PrintSet(set<int>& s)
{
	for(auto s1:s)
		cout<<s1<<" ";
	cout<<endl;
}

void text()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	
	//10 20 30 40
	
	set<int>::iterator s_begin = s1.begin();
	set<int>::iterator s_end = s1.end();
	
	s_begin++;
	s_begin++;
	s_end--;
	
	s1.erase(s_begin,s_end); //相当于删除[2,3) 
	PrintSet(s1);
}


int main()
{
	text();
}
#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);
	p(s1);
     //erase删除
	s1.erase(s1.begin());
	p(s1);
	//删除某个元素
	//类似list容器中的remove
	s1.erase(5);
	p(s1);
	//删除某段区间数据
	s1.erase(++s1.begin(), s1.end());
	p(s1);
	//清空容器
	s1.clear();
	p(s1);
}
int main()
{
	test();
	system("pause");
	return 0;
}


5.set查找和统计

函数原型:

find(key); //查找key是否存在,返回该键的迭代器,若不存在返回set.end();
count(key);//统计key元素个数
#include<iostream>
#include<set>
using namespace std;

void PrintSet(set<int>& s)
{
	for(auto s1:s)
		cout<<s1<<" ";
	cout<<endl;
}

void text()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(30);
	s1.insert(30);
	s1.insert(20);
	
	int S_count = s1.count(30);
	//1
	cout<<S_count<<endl;
	
	set<int>::iterator pos = s1.find(30);
	if(pos!=s1.end()) cout<<*pos<<endl;
	
	
}


int main()
{
	text();
}

6.pair队组的创建

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

两种创建方式:

pair<type,type> p (v1,v2);
pair<type,type> p = make_pair(v1,v2);
#include<iostream>
#include<cstring>
using namespace std;

void text()
{
	pair<string,int> p(string("Tom"),20);
	cout<<p.first<<" "<<p.second<<endl;
	
	pair<string,int> p1=make_pair("Jerry",21);
	cout<<p1.first<<" "<<p1.second<<endl;
	
}


int main()
{
	text();
}

7.set容器排序

set容器默认排序为从小到大,利用仿函数,可以改变排序规则.

非结构体类型

#include<iostream>
#include<set>
#include<cstring>
using namespace std;

struct MyCompare
{

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

void PrintSet(set<int,MyCompare>& s)
{
	for(auto s1:s)
		cout<<s1<<" ";
	cout<<endl;
}

void text()
{
	set<int,MyCompare> s;
	s.insert(10);
	s.insert(20);
	s.insert(30);
	s.insert(40);
	PrintSet(s);
	//40 30 20 10
	
}


int main()
{
	text();
}

结构体类型:

#include <iostream>
#include <set>
#include <iterator>
#include <string>
using namespace std;
struct My	//重载操作符<,自定义排序规则
{
	string name;
	float score;
	friend bool operator< (const My &a,const My &b)
	{
		return a.score>b.score;	//按score由大到小排列
	}
};
 
int main()
{
	set<My> s;
	My my;
	my.name = "Tom";
	my.score = 18.5;
	s.insert(my);
	my.name = "lili";
	my.score = 19.0;
	s.insert(my);
	my.name = "My";
	my.score = 17.0;
	s.insert(my);
	my.name = "ben";
	my.score = 18.0;
	s.insert(my);
	
	set<My>::iterator it;
	for(it = s.begin(); it != s.end(); it++)
	{
		cout<<(*it).name<<" : "<<(*it).score<<endl;
	}
		return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值