set

set
set_第1张图片
multiset
set_第2张图片
序列式怎么插入的怎么输出,关联式会排序,从小到大

void test1()
{
	set<int> s1;
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	printset(s1);// 2 3 4 5

	if (s1.empty())
	{
		cout << "kong" << endl;
	}
	else {
		cout << "bukong" << endl;
	}
	s1.erase(s1.begin());//删第一个 3 4 5
	s1.erase(3);//删除元素3 得到 4 5
	printset(s1);
}

set_第3张图片

//查找
void test2()
{
	set<int> s1;
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	printset(s1);// 2 3 4 5

	//对于set 没有value  key就是value
	set<int>::iterator  pos = s1.find(3);
	//判断是否找到
	if (pos != s1.end())
	{
		cout << "找到了,值为" << *pos << endl;
	}
	else {
		cout << "没找到" << endl;
	}
	//count(key)查找key元素的个数,对于set而言 结果0或者1
	int num=s1.count(3);

	//lower_bound(keyElem)  //返回第一个key>=keyElem元素的迭代器
	set<int>::iterator it= s1.lower_bound(3);
	if (it != s1.end())
	{
		cout <<"找到了"<< *it << endl;

	 }
	else {
		cout << "无" << endl;
	}
	//upper_bound(keyElem)  返回第一个key>keyElem元素的迭代器
	set<int>::iterator it1 = s1.upper_bound(3);
	if (it1 != s1.end())
	{
		cout << "找到了" << *it1 << endl;

	}
	else {
		cout << "无" << endl;
	}
	//equal_range(keyElem)返回容器中key与keyElem相等的上下限的两个迭代器
	//上下限 就是upper_bound lower_bound
	s1.equal_range(4);
	pair<set<int>::iterator, set<int>::iterator> ret= s1.equal_range(4);
	//ret.first;//第一个迭代器
	if (ret.first != s1.end())
	{
		cout << "下限是" << *(ret.first)<< endl;
	}
	else {
		cout << "没找到" << endl;
	}
	if (ret.second != s1.end())
	{
		cout << "上限是" << *(ret.second) << endl;
	}
	else {
		cout << "没找到" << endl;
	}

}

pair

// pair.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
using namespace std;
void test01()
{
	//第一种创建方式
	pair<string, int> p(string("aaa"), 100);
	cout << "姓名" << p.first << endl;
	cout << "年龄" << p.second << endl;
	//第二种创建方式
	pair<string, int> p2 = make_pair("bbb", 200);
	cout << "姓名" << p2.first << endl;
	cout << "年龄" << p2.second << endl;



}

int main()
{
	test01();
    return 0;
}


set不允许插入重复的值

//set不允许插入重复的值
void test3()
{
	set<int> s1;
	pair<set<int>::iterator,bool> ret= s1.insert(10);
	if (ret.second)
	{
		cout<<"插入成功 "<< endl;
	}
	else {
		cout << "插入失败" << endl;
	}
	ret = s1.insert(10);
	if (ret.second)
	{
		cout << "插入成功 " << endl;
	}
	else {
		cout << "插入失败" << endl;
	}
	s1.insert(10);
	printset(s1);//10 只有一个
	//multiset 允许插入重复值
	mul.insert(10);
	mul.insert(10);//两个都插入成功
}

排序

void test04()
{
	//set s1;
	set<int, myCompare>s1;//因为需要放入类型 ,所以是用仿函数
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	//printset(s1);// 2 3 4 5
	//想要从大到小排序
	//在插入之前制定排序规则
	for (set<int, myCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

set容器插入自定义类型数据

class Person{
public:
	Person(string name, int age)
	{
		this->age = age;
		this->name = name;

	}
public:
	string name;
	int age;
};
//仿函数
class comPerson
{
public:
	bool operator()(const Person &p1, const Person & p2)
	{
		return p1.age > p2.age;
	}
};
void test5()
{
	set<Person, comPerson> s1;
	Person p1("aaa", 1);
	Person p2("bbb", 2);
	Person p3("ccc", 3);
	
	//s1.insert(p1);
	//s1.insert(p2);
	//s1.insert(p3);
	//插入失败 因为插入时 set容器不知道怎么对它排序
	//插入自定义类型,要先制定好排序规则
	//排序规则写在	set s1 内

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	for (set<Person, comPerson>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "姓名" << (*it).name << "   年龄" << (*it).age << endl;
	}
}

你可能感兴趣的:(set)