2021-8-18 C++提升学习 -> 3.8、set容器

目录

3.8.2、构造和赋值

3.8.3、大小和交换

3.8.4、插入和删除

3.8.5、查找和统计

3.8.6、set与multiset区别

3.8.7、pair使用

3.8.8、内置类型指定排序顺序规则

3.8.9、自定义数据类型指定排序规则


3.8.2、构造和赋值

set容器特点:所有元素插入的时候回自动排序

set容器不允许插入重复的值

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

	//遍历容器
	//set容器特点:所有元素插入的时候回自动排序
	//set容器不允许插入重复的值
	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//赋值操作
	set<int>s3 = s2;
	printSet(s3);
}

int main()
{

	test01();
	system("pause");
}

3.8.3、大小和交换

统计大小——size 判断是否为空——empty 插入元素——insert

#include <iostream>
#include <string>
#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;

	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;
	}



}

//交换
void test02()
{
	set<int>s1;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set<int>s2;

	s2.insert(100);
	s2.insert(200);
	s2.insert(300);
	s2.insert(400);
	
	cout << "交换前:" << endl;
	printSet(s1);
	printSet(s2);

	s1.swap(s2);

	cout << "交换后:" << endl;
	printSet(s1);
	printSet(s2);
}
int main()
{
	test02();
	test01();
	system("pause");
}

3.8.4、插入和删除

insert——插入 erase——删除 clear——清空

#include <iostream>
#include <string>
#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;

	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);

	//遍历
	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

	//删除重载版本
	s1.erase(40);
	printSet(s1);

	//清空
	s1.erase(s1.begin(), s1.end());

	s1.clear();


}

int main()
{

	test01();
	system("pause");
}

3.8.5、查找和统计

对于set而言统计的结果要么是0要么是1

int num = s1.count(30);

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

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

void test01()
{
	set<int>s1;

	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);

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

void test02()
{
	set<int>s1;

	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);

	int num = s1.count(30);
	//对于set而言统计的结果要么是0要么是1
	cout <<"num = "<< num << endl;
}

int main()
{
	test02();
	test01();
	system("pause");
}

3.8.6、set与multiset区别

如果不允许插入重复的数据使用set

如果需要使用重复数据使用multiset

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

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>s2;
	//允许插入重复值
	s2.insert(10);
	s2.insert(10);
	s2.insert(10);
	s2.insert(10);
	for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it <<"  ";
	}
	cout << endl;

}


int main()
{

	test01();
	system("pause");
}

3.8.7、pair使用

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

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

	cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
	
	//第二种方式
	pair<string, int>p2 = make_pair("Jerry",3);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

int main()
{

	test01();
	system("pause");
}

3.8.8、内置类型指定排序顺序规则

利用仿函数可以指定set容器中的排序规则

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

class MyCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};

void test01()
{
	set<int>s1;

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

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

	}
	cout << endl;
}


int main()
{

	test01();
	system("pause");
}

3.8.9、自定义数据类型指定排序规则

自定义的数据类型都会指定排序规则

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

class person
{
public:
	person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

	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 test01()
{
	//自定义的数据类型都会指定排序规则
	set<person, Compareperson>s;

	person p1("刘备", 24);
	person p2("关羽", 28);
	person p3("张飞", 25);
	person p4("赵云", 21);

	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->m_Name << " 年龄: " << it->m_Age << endl;
	}
}

int main()
{

	test01();
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值