set容器

13 篇文章 0 订阅
11 篇文章 0 订阅

恭喜主教大人完成了自己的目标!!! (´▽`)

set 容器

set容器基本概念

简介:

  • 所有元素都会在插入时所有元素都会在插入时自动被排序(自动去重/可重复插不报错但是去重了,默认从小到大排)

本质:

  • set/multiset属于关联式容器,底层结构是用二叉树实现

set和multiset区别

  • set不允许容器中有重复的元素

  • multiset允许容器中有重复的元素

set容器的使用(表格)

函数意义使用
insert()set容器唯一的插入数据方式a.insert(c);
size()判断容器中元素的数目cout << "d容器中有元素" << d.size() << "个" << endl;
empty()判断容器是否为空if (d.empty()) { cout << "d容器为空" << endl; } else { cout << "容器不为空" << endl; cout << "d容器中有元素" << d.size() << "个" << endl; }
swap(st)交换两个集合容器a.swap(d);
insert()在容器中插入元素a.insert(c);
clear()清除所有元素a.clear();
erase(pos)删除pos迭代器所指的元素,返回下一个元素的迭代器(删小的)a.erase(a.begin());
erase(beg,end)删除区间(beg(),end())的所有元素,返回下一个元素的迭代器a.erase(a.begin(), a.end());
erase(elem)删除容器中值为elem的元素a.erase(b);
find(key)查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end();if (v.find(g) == v.end()) { cout << "无该元素" << endl; } else { cout << "存在该元素" << endl; } set<int>::iterator a = v.find(g);
count(key)统计key的元素个数(0或者1)cout << "v容器中的元素数" << v.count(x) << endl;

set构造和赋值

功能描述:创建set容器以及赋值

 

set赋值

 

#include<iostream>
#include<set>
using namespace std;
​
void printset(const set<int>&a)
{
    for (set<int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << *it << "  " ;
    }
    cout << endl;
}
​
void insert(set<int>& a)
{
    int b;
    cout << "输入要插入树中的元素数" << endl;
    cin >> b;
    for (int i = 0; i < b; i++) 
    {
        int c;
        cout << "输入要存入的数" << endl;
        cin >> c;
        a.insert(c);
    }
}
int main()
{
    set<int>a;
    insert(a);
    printset(a);
​
    set<int>b;
    b = a;
    printset(b);
​
    set<int>c(b);
    printset(c);
​
​
    system("pause");
    return 0;
}

set大小和交换

功能描述:

  • 统计set容器大小以及交换set容器

 

#include<iostream>
#include<set>
using namespace std;
​
void printset(const set<int>&a)
{
    for (set<int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << *it << "  " ;
    }
    cout << endl;
}
​
void insert(set<int>& a)
{
    int b;
    cout << "输入要插入树中的元素数" << endl;
    cin >> b;
    for (int i = 0; i < b; i++) 
    {
        int c;
        cout << "输入要存入的数" << endl;
        cin >> c;
        a.insert(c);
    }
}
int main()
{
    set<int>a;
    insert(a);
    printset(a);
​
    set<int>b;
    b = a;
    printset(b);
​
    set<int>c(b);
    printset(c);
​
    set<int>d;
    insert(d);
    printset(d);
​
    if (a.empty()) 
    {
        cout << "a容器为空" << endl;
    }
    else 
    {
        cout << "容器不为空" << endl;
        cout << "a容器中有元素" << a.size() << "个" << endl;
    }
    
    if (d.empty())
    {
        cout << "d容器为空" << endl;
    }
    else
    {
        cout << "容器不为空" << endl;
        cout << "d容器中有元素" << d.size() << "个" << endl;
    }
​
    a.swap(d);
    printset(a);
    printset(d);
    system("pause");
    return 0;
}

set插入和删除

功能描述:

  • set容器进行插入数据和删除数据

 

#include<iostream>
#include<set>
using namespace std;
​
void printset(const set<int>&a)
{
    for (set<int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << *it << "  " ;
    }
    cout << endl;
}
​
void insert(set<int>& a)
{
    int b;
    cout << "输入要插入树中的元素数" << endl;
    cin >> b;
    for (int i = 0; i < b; i++) 
    {
        int c;
        cout << "输入要存入的数" << endl;
        cin >> c;
        a.insert(c);
    }
}
​
void deleteset(set<int>& a)
{
    int b;
    cout << "输入要删除树中的元素数" << endl;
    cin >> b;
    for (int i = 0; i < b; i++)
    {
        a.erase(a.begin());
    }
}
​
​
void deleteset2(set<int>& a)
{
    int b;
    cout << "输入要删除树中的元素" << endl;
    cin >> b;
    a.erase(b);
}
void deleteset3(set<int>&a)
{
    a.erase(a.begin(), a.end());
}
int main()
{
    set<int>a;
    insert(a);
    printset(a);
​
    set<int>b;
    b = a;
    printset(b);
​
    set<int>c(b);
    printset(c);
​
    set<int>d;
    insert(d);
    printset(d);
​
    if (a.empty()) 
    {
        cout << "a容器为空" << endl;
    }
    else 
    {
        cout << "容器不为空" << endl;
        cout << "a容器中有元素" << a.size() << "个" << endl;
    }
    
    if (d.empty())
    {
        cout << "d容器为空" << endl;
    }
    else
    {
        cout << "容器不为空" << endl;
        cout << "d容器中有元素" << d.size() << "个" << endl;
    }
​
    a.swap(d);
    printset(a);
    printset(d);
​
    a.clear();
    printset(a);
​
    deleteset(d);
    printset(d);
​
    deleteset2(d);
    printset(d);
​
    deleteset3(d); 
    printset(d);
​
    system("pause");
    return 0;
}

set容器查找和统计

功能描述:

  • 对set容器进行查找数据以及统计数据

函数原型:

 

#include<iostream>
#include<set>
using namespace std;
​
void printset(const set<int>& a)
{
    for (set<int>::iterator it = a.begin(); it != a.end(); it++)
    {
        cout << *it << "  ";
    }
    cout << endl;
}
​
void insert(set<int>& a)
{
    int b;
    cout << "输入要插入树中的元素数" << endl;
    cin >> b;
    for (int i = 0; i < b; i++)
    {
        int c;
        cout << "输入要存入的数" << endl;
        cin >> c;
        a.insert(c);
    }
}
​
​
int main()
{
    set<int>v;
    insert(v);
    printset(v);
    int g;
    cout << "输入要查找的元素" << endl;
    cin >> g;
    if (v.find(g) == v.end())
    {
        cout << "无该元素" << endl;
    }
    else
    {
        set<int>::iterator a = v.find(g);
        cout << "存在该元素" << endl;
    }
​
​
    int x;
    cout << "输入要查找的元素数" << endl;
    cin >> x;
    cout << "v容器中的元素数" << v.count(x) << endl;
​
    system("pause");
    return 0;
}

set 和multiset区别

区别:

  • set不可以插入重复数据,而multiset可以

  • set插入数据的同时会返回插入结果,表示插入是否成功

  • multiset不会检测数据,因此可以插入重复数据

 
pair<set<int>::iterator,bool> a=s.insert(10);
if(a.second)
{
    cout<<"第一次插入成功"<<endl;
}else
{
    cout<<"第一次插入失败"<<endl;
}
a=s.insert(10);
if(a.second)
{
    cout<<"第二次插入成功"<<endl;
}else
{
    cout<<"第二次插入失败"<<endl;
}
multiset<int>b;
b.insert(10);
if(b.second)
{
    cout<<"第一次插入成功"<<endl;
}else
{
    cout<<"第一次插入失败"<<endl;
}
b.insert(10);
if(b.second)
{
    cout<<"第二次插入成功"<<endl;
}else
{
    cout<<"第二次插入失败"<<endl;
}
​
for(multiset<int>::iterator it=b.begin;it!=b.end();it++)
{
    cout<<*it<<endl;
}

set容器排序

  • set容器默认排序规则为从小到大,掌握如何改变排序规则

主要技术:

  • 利用仿函数,可以改变排序规则(仿函数重载函数调用的())

    1.内置类型指定排序规则2.自定义数据类型排序规则

#include<iostream>
#include<set>
using namespace std;
​
class mycompare
{
    public:
    bool operator()(模板列表中数据类型 a,模板列表中数据类型 b)const
    {
        return a>b;
    }
};
​
​
void printset(set<int>a)
{
    for(set<int>::const_iterator it=a.begin();it!=a.end();it++)
{
        cout<<*it<<"  ";
    }
    cout<<endl;
}
void pushset(set<int>&a)
{
    int b;
    cout<<"输入要存入的数量"<<endl;
    cin>>b;
    for(int i=0;i<b;i++)
    {
        int c;
        cout<<"输入要存入的数据"<<endl;
        cin>>c;
        a.insert(c);
    }
}
int main()
{
    set<int>a;
    pushset(a);
    printset(a);
    
    set<int,mycompare>b;
    pushset(b);
    //printset(b);这个行不通了
    for(set<int,mypare>::iterator it=b.begin();it!=b.end();it++)
    {
        cout<<*it<<"  ";
    }
    cout<<endl;
    system("pause");
    return 0;
}
  • 自定义数据类型通常指定排序规则

失败版本
#include<iostream>
#include<set>
#include<string>
using namespace std;
​
class juese 
{
public:
    string name;
    string yuanshu;
    //int mingzuo;
    string wuqi;
    int zhanlijieji;
​
    juese() 
    {
        cout << "输入角色姓名" << endl;
        cin >> name;
        cout << "输入角色属性" << endl;
        cin >> yuanshu;
        //cout << "输入角色核心命坐" << endl;
        //cin >> mingzuo;
        cout << "输入角色武器" << endl;
        cin >> wuqi;
        cout << "输入角色强度阶级" << endl;
        cin >> zhanlijieji;
    }
    void printset2()   //遍历内置的
    {
        cout << "角色" << name << "是" << yuanshu << "属性角色" <<"使用"<<wuqi<< "武器" << endl;
    }
    /*bool operator()(juese a, juese b)
    {
        return a.zhanlijieji>b.zhanlijieji;
    }*/
};
​
class compare
{
public:
    bool operator()(const juese a, const juese b)const
    {
        return a.zhanlijieji > b.zhanlijieji;
    }
};
​
void printset(set<juese,compare>&a)  //遍历外面的
{
    for (set<juese, compare>::const_iterator it = a.begin(); it!= a.end(); it++)
    {
        cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" <<it->wuqi << "武器" << endl;
    }
}
​
void pushinsert(set<juese,compare>&a)
{
    juese b;
    a.insert(b);
}
​
​
int main() 
{
    int d;
    cout << "输入要存入多少角色信息" << endl;
    cin >> d;
    set<juese,compare>a;
​
    for (int i = 0; i < d; i++) 
    {
        pushinsert(a);
    }
    printset(a);
​
​
​
    system("pause");
    return 0;
}
​
#include<iostream>
#include<set>
#include<string>
using namespace std;
​
class juese 
{
public:
    string name;
    string yuanshu;
    //int mingzuo;
    string wuqi;
    int zhanlijieji;
​
    juese() 
    {
        cout << "输入角色姓名" << endl;
        cin >> name;
        cout << "输入角色属性" << endl;
        cin >> yuanshu;
        //cout << "输入角色核心命坐" << endl;
        //cin >> mingzuo;
        cout << "输入角色武器" << endl;
        cin >> wuqi;
        cout << "输入角色强度阶级" << endl;
        cin >> zhanlijieji;
    }
    /*void printset2()   //遍历内置的
    {
        cout << "角色" << name << "是" << yuanshu << "属性角色,核心命坐为" << mingzuo << "命,使用"<<wuqi<< "武器" << endl;
    }
    bool operator()(juese a, juese b)
    {
        return a.zhanlijieji>b.zhanlijieji;
    }*/
};
​
class compare
{
public:
    bool operator()( juese &a,  juese &b)
    {
        /*if (a.zhanlijieji == b.zhanlijieji)
        {
            return a.mingzuo > b.mingzuo;
        }
        else 
        {
            return a.zhanlijieji > b.zhanlijieji;
        }*/
        return a.zhanlijieji > b.zhanlijieji;
        
    }
};
​
int main() 
{
    set<juese,compare>a;
    juese b;
    juese c;
    a.insert(b);
    a.insert(c);
    for (set<juese,compare>::iterator it = a.begin(); it != a.end(); it++)
    {
        cout << "角色" << it->name << "是" << it->yuanshu << "属性角色"  << "使用" << it->wuqi << "武器" << endl;
    }
​
​
​
    system("pause");
    return 0;
}
成功版本
#include<iostream>
#include<set>
#include<string>
using namespace std;
​
class juese
{
public:
    string name;
    string yuanshu;
    int mingzuo;
    string wuqi;
    int zhanlijieji;
​
    juese()
    {
        cout << "输入角色姓名" << endl;
        cin >> name;
        cout << "输入角色属性" << endl;
        cin >> yuanshu;
        cout << "输入角色核心命坐" << endl;
        cin >> mingzuo;
        cout << "输入角色武器" << endl;
        cin >> wuqi;
        cout << "输入角色强度阶级" << endl;
        cin >> zhanlijieji;
    }
    /*void printset2()   //遍历内置的
    {
        cout << "角色" << name << "是" << yuanshu << "属性角色,核心命坐为" << mingzuo << "命,使用"<<wuqi<< "武器" << endl;
    }
    bool operator()(juese a, juese b)
    {
        return a.zhanlijieji>b.zhanlijieji;
    }*/
};
​
class compare
{
public:
    bool operator()(juese a, juese b)const
    {
        if (a.zhanlijieji == b.zhanlijieji)
        {
            return a.mingzuo > b.mingzuo;
        }
        else
        {
            return a.zhanlijieji > b.zhanlijieji;
        }
        return a.zhanlijieji > b.zhanlijieji;
​
    }
};
​
void printset(set<juese,compare>const &a)  //遍历外面的
{
    for (set<juese, compare>::const_iterator it = a.begin(); it != a.end(); it++)
    {
        cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" << it->wuqi << "武器" << endl;
    }
}
​
​
int main()
{
    set<juese,compare>a;
    juese b;
    juese c;
    a.insert(b);
    a.insert(c);
    for (set<juese, compare>::const_iterator it = a.begin(); it != a.end(); it++)
    {
        cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" << it->wuqi << "武器" << endl;
    }
​
​
​
    system("pause");
    return 0;
}

总结

  • bool operator()(juese a, juese b)const { if (a.zhanlijieji == b.zhanlijieji) { return a.mingzuo > b.mingzuo; } else { return a.zhanlijieji > b.zhanlijieji; } 注意 函数后加const 且 自定义类型不能加引用

  • for (set<juese, compare>::const_iterator it = a.begin(); it != a.end(); it++)

注意const必须有 没有跑不下来

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只符华单推人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值