set/multiset(关联式)容器,对组,set不能重复插入的原理,map/multimap容器

set容器中的数字没有重复的,因为键值就是实值。set底层是用红黑树实现的,根据键值可以定位一个元素

#include<iostream>
#include<set>
using namespace std;
//关联式容器,只要插入数据就自动排序
void printfs(set<int>& p)
{
    for (set<int>::iterator it = p.begin(); it != p.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
void test()
{
    set<int>v;
    v.insert(1); //插入数据只能用insert
    v.insert(7);
    v.insert(3);
    v.insert(9);
    v.insert(5);
    printfs(v);          //1 3 5 7 9 默认从小到大进行排序
    v.erase(++v.begin());    //不支持随机访问迭代器  1 5 7 9 
    v.erase(7);          //可以直接通过实值进行删除 1 5 9
    printfs(v);
}
int main()
{
    test();
    return 0;
}

#include<iostream>
#include<set>
using namespace std;
void test1()
{
    set<int>v;
    v.insert(1); 
    v.insert(7);
    v.insert(3);
    v.insert(9);
    v.insert(5);
    set<int>::iterator pos = v.find(3);
    if (pos != v.end())
    	cout << "找到了" << endl;
    else
    	cout << "没找到" << endl;
    //因为set容器下没有重复的值所以count要么是0,要么是1
    int counts = v.count(5);
    cout << "v.count(5)的个数就是" << counts << endl;
    set<int>::iterator s = v.lower_bound(10);
    //lower_bound(set)返回第一个 >=set的数的迭代器,找不到的话就返回v.set()(也就是容器最后一个元素的下一个位置)
    if (s != v.end())
    	cout << "找到了" << *s << endl;
    else
    	cout << "没找到" << endl;
    //upper_bound(set)返回第一个 >set的数的迭代器,找不到的话就返回v.set()
    set<int>::iterator s1 = v.upper_bound(5);    
    if (s1 != v.end())
    	cout << "找到了" << *s1 << endl;
    else
    	cout << "没找到" << endl;
    //equal_range(set)会返回两个返回值(迭代器),set的上限和下限也就是lower_bound(set),upper_bound(set)
    //这时候就需要深入底层去看看怎样接受两个返回值,对组pair<iterator, iterator>
    //pair<iterator, iterator>这个是一种类型
    pair<set<int>::iterator , set<int>::iterator> L = v.equal_range(3);
    //这样就相当于定义好了变量L用来获取返回值 
    if (L.first != v.end())
    	cout << "找到了 lower_bound(set)= " << *(L.first) << endl;
    else
    	cout << "没找到" << endl;
    if (L.second != v.end())
    	cout << "找到了 upper_bound(set)= " << *(L.second) << endl;
    else
	cout << "没找到" << endl;
}
int main()
{
    test1();
    return 0;
}





结果:
找到了
v.count(5)的个数就是1
没找到
找到了7
找到了 lower_bound(set)= 3
找到了 upper_bound(set)= 5
请按任意键继续. . .


//对组的两种创建方式
#include<iostream>
#include<set>
#include<string>
using namespace std;
void test2()
{
    ///第一种创建对组的方式
    pair<string, int> p("王者", 10);
    cout << p.first << " " << p.second << endl;
    ///第二种创建对组的方式
    pair<string, int> p1 = make_pair("王者", 10);
    cout << p1.first << " " << p1.second << endl;
}
int main()
{
    test2();
    return 0;
}
#include<iostream>
#include<set>
#include<string>
void test3()
{
    //关于insert不允许插入重复键值的深究
    set<int>v;
    v.insert(1);
    v.insert(1);
    printfs(v);        //可以运行成功但是只打印一个1
    //首先深入底层查看insert函数返回值原型
    ///_Pairib insert(value_type&& _Val)
    //typedef typename _Mybase::_Pairib _Pairib;
    //typedef pair<iterator, bool> _Pairib;   //返回值原函数
    //可知返回值是对组类型,我们进行检测一下
    pair<set<int>::iterator, bool>  _Pairib;
    _Pairib = v.insert(10);
    if (_Pairib.second)
    {
    	cout << "第一次插入成功" << endl;
    }
    else
    {
    	cout << "第一次插入失败" << endl;
    }
    _Pairib = v.insert(10);
    if (_Pairib.second)
    {
    	cout << "第二次插入成功" << endl;
    }
    else
    {
    	cout << "第二次插入失败" << endl;
    }
}
int main()
{
    test3();
    return 0;
}



结果:
1
第一次插入成功
第二次插入失败
请按任意键继续. . .

 如果使用multiset第二次插入也会成功,并且可以成功打印出来,这就是set和multiset的区分

#include<iostream>
#include<set>
#include<string>
void printfs(set<int>& p)
{
    for (set<int>::iterator it = p.begin(); it != p.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
//从大到小排序set容器
//由于默认是从小到大,但是插入数据之后就不会再被允许修改,所以要想从大到小排序就必须在插入数据前就指定好排序规则(利用仿函数)
class person
{
public:
    int operator() (int a, int b)       //重载()不要问为什么因为我也不知道
    {
    	return a > b;
    }

};
void printfs2(set<int, person>& p)
{
    for (set<int, person>::iterator it = p.begin(); it != p.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
void test4()
{
    set<int>v;
    v.insert(1);
    v.insert(7);
    v.insert(3);
    v.insert(9);
    v.insert(5);
    printfs(v);   //默认是从小到大打印
    set<int, person>v1;
    v1.insert(1);
    v1.insert(7);
    v1.insert(3);
    v1.insert(9);
    v1.insert(5);
    printfs2(v1);
}

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




结果:
1
第一次插入成功
第二次插入失败
请按任意键继续. . .

为什么使用仿函数(类)而不直接使用函数,是因为<>内只能填写类型名而不是函数名。

set插入自定义数据类型:

//自定义数据类型做插入
class person
{
public:
    person(string name, int ages)
    {
    	this->ages = ages;
    	this->name = name;
    }
    string name;
    int ages;
};
class sorts           //伪函数必须重新写一个类
{
public:
    bool operator() (const person& a, const person& b)       //重载()不要问为什么因为我也不知道
    {
    	return a.ages > b.ages;
    }
};
void printfs(set<person,sorts>& p)
{
    cout << "-----------------" << endl;
    for (set<person,sorts>::iterator it = p.begin(); it != p.end(); ++it)
    {
    	cout << "|  "<< it->name <<"\t"<< "|  " << (*it).ages <<"\t"<< "| " << endl;
    	cout << "-----------------" << endl;
    }
}
void test5()
{
    set<person,sorts>v;
    person p1("大娃", 100);
    person p2("2娃", 90);
    person p3("3娃", 80);
    person p4("4娃", 70);
    person p5("5娃", 60);
    person p6("6娃", 50);
    person p7("7娃", 40);
    person p8("爷爷", 30);
    v.insert(p1);
    v.insert(p2);
    v.insert(p3);
    v.insert(p4);
    v.insert(p5);
    v.insert(p6);
    v.insert(p7);
    v.insert(p8);
    printfs(v);
}
int main()
{
    test5();
    return 0;
}



结果:
-----------------
|  大娃 |  100  |
-----------------
|  2娃  |  90   |
-----------------
|  3娃  |  80   |
-----------------
|  4娃  |  70   |
-----------------
|  5娃  |  60   |
-----------------
|  6娃  |  50   |
-----------------
|  7娃  |  40   |
-----------------
|  爷爷 |  30   |
-----------------
请按任意键继续. . .

#include<iostream>
#include<map>
#include<string>
using namespace std;
void printfs(map<int,string>& p)
{
    for (map<int, string>::iterator it = p.begin(); it != p.end(); ++it)
    {
    	cout << (*it).second << endl;
    }
}
void test()
{
    map<int, string>v;
    //四种插入数据的方式
    v.insert(pair<int,string>(1, "西瓜"));
    v.insert(make_pair(2, "南瓜"));          //推荐
    v.insert(map<int, string>::value_type(3, "冬瓜"));
    v[4] = ("北瓜");
    v[5];          //如果不做数据插入那么系统会自动给你插入一个0
    printfs(v);
}
int main()
{
    test();
    return 0;
}

一个小案例:给五个员工进行分组

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<time.h>
using namespace std;
enum{吃饭,拉屎,吃屎};
class person
{
public:
    person(string name,int money)
    {
	this->name = name;
    	this->money = money;
    }
    string name;
    int money;
};
void chushihua(vector<person>& p)
{
    string name1 = "员工";
    string  A = "ABCDE";
    for (int i = 0; i < 5; ++i)
    {
    	string name2 = name1 + A[i];
    	int money = rand() % 10000 + 10000;
    	person s(name2, money);
    	p.push_back(s);
    }
}
void printfs(vector<person>& p)
{
    for (vector<person>::iterator it = p.begin(); it != p.end(); ++it)
    {
        cout << it->name << " " << it->money << endl;
    }
}
void Fenzu(vector<person>& p1,multimap<int,person>&p2)
{
    for (vector<person>::iterator it = p1.begin(); it != p1.end(); ++it)
    {
	int num = rand() % 3;
	p2.insert(make_pair(num, *it));
    }
}

void func(multimap<int,person>& p)
{
    multimap<int, person>::iterator it = p.find(吃饭);
    int a = 0;
    int num = p.count(吃饭);
    cout << "吃饭部门员工" << endl;
    for (it; a++ < num; ++it)  //1 A B | 2 C D|3 E
    {
    	cout << "姓名 :" << (*it).second.name << " " << "工资 :" << (*it).second.money << endl;
    }
    cout << "-------------------------------" << endl;
    it = p.find(拉屎);
    a = 0;
    num = p.count(拉屎);
    cout << "拉屎部门员工" << endl;
    for (it; a++ < num; ++it)  //1 A B | 2 C D|3 E
    {
	cout << "姓名 :" << (*it).second.name << " " << "工资 :" << (*it).second.money << endl;
    }
    cout << "---------------------------------" << endl;
    it = p.find(吃屎);
    a = 0;
    num = p.count(吃屎);
    cout << "吃屎部门员工" << endl;
    for (it; a++ < num; ++it)  //1 A B | 2 C D|3 E
    {
	    cout << "姓名 :" <<  (*it).second.name << " " <<"工资 :"<< (*it).second.money << endl;
    }
}
int main()
{
    srand((unsigned int)time(NULL));
    //初始化五名员工
    vector<person>v;
    chushihua(v);
    //分组
    multimap<int, person>M;      //这里员工分组选择的是multimap容器,是因为multimap容器一个key值(组)可以容纳多个实值(员工)
    Fenzu(v, M);
    //按照分组打印
    func(M);
    return 0;
}









结果:
吃饭部门员工
姓名 :员工D 工资 :11458
姓名 :员工E 工资 :15997
-------------------------------
拉屎部门员工
姓名 :员工A 工资 :10304
姓名 :员工C 工资 :11768
---------------------------------
吃屎部门员工
姓名 :员工B 工资 :13398
请按任意键继续. . .

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值