STL set相关学习笔记

来源:互联网 发布:数据挖掘需要什么基础 编辑:程序博客网 时间:2024/05/14 20:23
#include <iostream>using namespace std;#include<set>/********************************************************************************bool fncomp (int lhs, int rhs){    return lhs<rhs;}class classcomp{    public:    bool operator() (const int& lhs, const int& rhs) const    {      return lhs>rhs;    }};int main(){    //set不支持:operator[]    //set构造函数://  1.用数组初始化:如下    int myints[]= {10,10,30,80,50};    set<int> second (myints,myints+5);//初始化的时候会把数组中重复的数字去掉,并自动排好序    set<int>::iterator s=second.begin();    while(s!=second.end())    {        cout<<*s<<" ";        s++;    }    //输出:10 30 50 80/////////////////////////////////////////////////////////////////    2.用另一个set对象初始化    set<int> third (second);    s=third.begin();    cout<<endl;    while(s!=third.end())    {        cout<<*s<<" ";        s++;    }    //输出:10 30 50 80////////////////////////////////////////////////////////////////  3.用一个set对象做初始化    set<int> fourth (second.begin(), --second.end());    s=fourth.begin();    cout<<endl;    while(s!=fourth.end())    {        cout<<*s<<" ";        s++;    }    //输出:10 30 50////////////////////////////////////////////////////////////////    int arr[]={1,2,3,4,6,6,7,8,5};    set<int,classcomp> fifth(arr,arr+9);//classcomp表示排序方式,用数组arr初始化set,去重并按照classcomp排序规则进行排序。    s=fifth.begin();    cout<<endl;    while(s!=fifth.end())    {        cout<<*s<<" ";        s++;    }    ////输出:8 7 6 5 4 3 2 1////////////////////////////////////////////////////////////////    bool(*fn_pt)(int,int) = fncomp;    set<int,bool(*)(int,int)> sixth (fn_pt);//参数必须作为函数指针。    sixth.insert(fifth.begin(),fifth.end());    s=sixth.begin();    cout<<endl;    while(s!=sixth.end())    {        cout<<*s<<" ";        s++;    }//////////////////////////////////////////////////////////////    return 0;}*********************************************************************************************/int main(){/**********************************    //begin()、end()、rbegin()、rend()函数:    int a[]={1,2,3,4,5};    set<int> s0(a,a+5);    set<int>::iterator it;    it=s0.begin();    it++;//不能这么写it+=1或者it=it+1。    //其他类似。************************************/    //clear():清空容器    int a[]={1,2,3,4,5};    set<int> s0(a,a+5);    s0.clear();//s0什么也没有了    //count()函数:    int a1[]={1,2,3,4,5};    set<int> s1(a1,a1+5);    cout<<s1.count(10);//查看10是不是容器中元素,不是返回0.    //empty():判断容器是否为空,空的话返回true,非空返回false.    bool falg=s1.empty();    //equal_range()函数:    pair<set<int>::const_iterator,set<int>::const_iterator>ret;    ret=s1.equal_range(4);    cout<<" "<<*ret.first;//输出:4    cout<<" "<<*ret.second;//输出:5    return 0;}int main(){    //insert()、erase()、find()    set<int> myset;    set<int>::iterator it;    for (int i=9; i>=1; i--)        myset.insert(i*10);//向容器中添加元素,并排序。返回值的类型为pair<iterator,bool>/****************************************************************    set<int> myset;    set<int>::iterator it;    for (int i=9; i>=1; i--)        myset.insert(i*10);    pair<std::set<int>::iterator,bool> ret;    ret = myset.insert(20);//插入会失败。    cout<<*ret.first<<endl;//输出20    cout<<ret.second<<endl;//输出0    myset.insert (it,25);//不管插入成功与否,都会返回it。*******************************************************************/    it = myset.begin();    ++it;    int a0[]={1,2,3,44,5};    set<int>s;    s.insert(a0,a0+5);//插入数组中的值,无返回值。    myset.erase (it);//把*it的值从容器中删除,没有返回值    cout<<endl<<myset.erase (60)<<" ";//从容器中找到40删除,返回1,没有这个元素返回0。    //iterator find (const value_type& val) const;    it = myset.find (40);//在容器中查找-3这个元素,找到返回iterator位置,找不到返回end()位置。    myset.erase (it, myset.end());//从it开始删除知道end()位置(不删除)结束    return 0;}int main(){/*************************************************************************************    //key_comp():原型 key_compare key_comp() const;    set<int> myset;    int highest;    set<int>::reverse_iterator rit;//不能这么判断rit<=myset.rend();只可以==或者!=    set<int>::key_compare mycomp = myset.key_comp();//key_comp()好像只能这么定义。    for (int i=0; i<=5; i++)        myset.insert(i);    set<int>::iterator it=myset.begin();//也不能这么判断it<=myset.end()    it++;    rit=myset.rbegin();    cout<<mycomp(*it,*it);//判断容器中两个元素的大小,若第一个小于或等于第二个,返回0,否则返回1***************************************************************************************************//***************************************************************    //lower_bound()、upper_bound()作用    //iterator lower_bound (const value_type& val) const;    //iterator upper_bound (const value_type& val) const;    set<int> myset;    set<int>::iterator itlow,itup;    for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90    itlow=myset.lower_bound (30); // 返回容器中第一个值【大于或等于】30的元素的iterator位置。    cout<<*itlow<<endl;    itup=myset.upper_bound (60); //返回容器中第一个值【大于】60的元素的iterator位置。    cout<<*itup<<endl;*******************************************************************//************************************************    //operator=、swap()、size():    int myints[]={ 12,82,37,64,15 };    set<int> first (myints,myints+5);    set<int> second;    second = first;//现在second是first,    first = std::set<int>();//现在first为空。    cout<<first.size()<<endl;//输出:0    cout<<second.size()<<endl;//输出:5//////////////////////////////////////////////////////    //void swap (set& x);    first.swap(second);//交换first与second    cout<<first.size()<<endl;//输出:5    cout<<second.size()<<endl;//输出:0**********************************************************///value_comp()函数//  value_compare value_comp() const;    set<int> myset;    set<int>::value_compare mycomp = myset.value_comp();    for (int i=0; i<=5; i++) myset.insert(i);    int highest=*myset.rbegin();    set<int>::iterator it=myset.begin();    cout<<highest<<" "<<*it<<endl;    cout<<mycomp(highest,*it);//比较两个数的大小,若第一个大于第二个,返回0;否则返回1.(参数只能是容器中的值,否则永远返回1)    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 脸太长额头太高怎么办 动车因台风停运怎么办 爸妈50了要离婚怎么办 鸿利彩票黑了钱怎么办 忘了锁屏图案怎么办 黄金被水银沾上怎么办 被股东了我该怎么办 异地恋没话题聊怎么办 谈了半年分手了怎么办 博士6年没毕业怎么办 发现孩子早恋家长应该怎么办 异地恋想嘿嘿嘿怎么办 妈妈溜冰溜大了怎么办 皮鞋被雨水泡了怎么办 老婆提出离婚我不想离怎么办 极度缺爱的人怎么办 生二胎住院大宝怎么办 爸妈偏心我该怎么办 无创21体高风险怎么办 无创检查高风险怎么办 唐氏筛查21三体高危怎么办 唐筛年龄高风险怎么办 21三体综合症高风险怎么办 朋友深陷李强365怎么办 飞机上烟瘾犯了怎么办 怀孕一个月吸烟了怎么办 烟瘾犯了没烟怎么办 押金交了不租了怎么办 买车首付款不够怎么办 双11订金不退怎么办 在商场买到假货怎么办 网上买到假手机怎么办 网上买了假手机怎么办 网银转账被骗了怎么办 支付宝被骗了钱怎么办 被支付宝骗了钱怎么办 头发出油怎么办小妙招 照相的时候脸歪怎么办 怀孕两个月同床了怎么办 小姐被警察抓了怎么办 我的世界迷路了怎么办