STL之set的基本使用

#include<iostream>
#include<set>
#include<cstdio>
#include<cstdlib>
using namespace std;


/*****************
*set中不允许重复插入,比如说插入两个一样的,只插入一个进去
******************/

/****************************
*  函数语法  :iterator begin()
                const_iterator begin() const
*  函数说明  :返回值为const_iterator时,set不可修改;
*  函数功能  :返回第一个元素的定位器的地址
*  函数返回值:返回一个指向第一个元素的双向定位器地址
******************************/

/****************************
*  函数语法  :iterator end()
                const_iterator end() const
*  函数说明  :set为空时,结果没定义
*  函数功能  :测试iterator是否到达尾部
*  函数返回值:返回一个指向最后一个元素的双向定位器地址
******************************/

/****************************
*  函数语法  :reverse_iterator rbegin()
                const_reverse_iterator begin() const
*  函数说明  :返回值为const_iterator时,set不可修改;
*  函数功能  :返回反向set的第一个元素的定位器的地址
*  函数返回值:返回一个反向set的指向第一个元素的双向定位器地址
******************************/

/****************************
*  函数语法  :reverse_iterator rend()
                const_reverse_iterator end() const
*  函数说明  :set为空时,结果没定义
*  函数功能  :返回一个指向反向set的最后元素后面的定位器
*  函数返回值:返回一个反向set的指向最后一个元素的双向定位器地址
******************************/

/****************************
*  函数语法  :void clear()
*  函数功能  :删除set中所有的元素
******************************/

/****************************
*  函数语法  :bool empty()const
*  函数功能  :判断set是否为空
*  函数返回值:为空返回true,反之返回false
******************************/

/****************************
*  函数语法  : iterator erase(iterator _Where)
               iterator erase(iterator _First,iterator _Last)
               iterator erase(const Key_type & _Key)
*  函数说明  : _Key是要删除的关键字的数值;
               _Where 删除元素的位置
               _First-_Last-1 删除元素的范围
*  函数功能  :删除一个挥着一定范围的元素
*  函数返回值:前两个函数返回指向第一个没被删除的元素的iterator
                第三个函数返回被删除的元素的个数
******************************/

/****************************
*  函数语法  : pair<iterator,bool>insert(const value_type& _Val)
               iterator erase(iterator _Where,const value_type& _Val)
               template<class InputIterator>void insert(InputIterator _First,InputIterator _Last)
*  函数说明  : _Val是要插入的关键字的数值;
               _Where 第一个插入元素的位置
               _First-_Last-1 插入元素的范围
*  函数功能  :插入一个或者一定范围的元素
*  函数返回值:第一个函数返回一对值,插入成功bool=true,如果元素已经存在 bool=false,iterator返回插入的位置或者已经存在的元素的位置
                第三个函数返回插入位置的定位器
******************************/

/****************************
*  函数语法  :size_type count(const Key& _Key)const
*  函数说明  :_Key是要进行匹配的关键字的数值;
*  函数功能  :返回_Key的个数
*  函数返回值:返回_Key的个数
******************************/

/****************************
*  函数语法  :iterator find(const Key& _Key)
                const_iterator find(const Key& _Key) const
*  函数功能  :返回第一个指向关键字的定位器
*  函数返回值:返回第一个指向关键字的定位器
******************************/

/****************************
*  函数语法  :size_type size()const
*  函数功能  :计算当前容器的元素个数
*  函数返回值:返回当前容器的长度
******************************/

/****************************
*  函数语法  :void swap(set &_Right)
*  函数功能  :交换两个容器的元素
******************************/

/****************************
*  函数语法  :size_type max_size()const
*  函数功能  :返回容器的最大可能长度
***/


/****************************
*  函数语法  :bool operator!=(
                  const set<Key,Type,Traits,Allocator>&_Left,
                  const set<Key,Type,Traits,Allocator>&_Right
                  )
                bool operator<(
                  const set<Key,Type,Traits,Allocator>&_Left,
                  const set<Key,Type,Traits,Allocator>&_Right
                  )
                同理还有==、<=、>=、>
*  函数功能  :(1)判断两个set:_Left和_Right是否不等
               (2)判断set:_Left是否小于_Right
*  函数返回值:(1)两个set不同时返回true,反之返回false
               (2)左边小于时返回true,反之返回false
***/


/****************************
*  函数语法  iterator lower_bound(const Key&_Key)const
              iterator upper_bound(...)  同上,只是大于
*  函数功能  :返回一个指向第一个关键字的值是大于等于_Key的iterator
*  函数返回值:返回一个指向第一个关键字的值是大于等于_Key的iterator~返回第一个大于等于_Key的位置
******************************/

set<char> m_Set;
set<int> m_Set1;
set<int> m_Set2;

void print(set<int> Set)
{
    set<int>::iterator iter=Set.begin();
    for(;iter!=Set.end();iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
}

void print1(set<int> Set)
{
    set<int>::reverse_iterator iter=Set.rbegin();
    for(;iter!=Set.rend();iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
}

int main()
{
    m_Set.insert('a');
    m_Set.insert('b');
    m_Set.insert('c');
    set<char>::iterator it;

    for(it=m_Set.begin();it!=m_Set.end();it++)
    {
        cout<<*it<<endl;
    }

        it==m_Set.end();
    it--;
    cout<<*it<<endl;


    m_Set.clear();

    if(m_Set.empty())
    {
        cout<<"Empty"<<endl;
    }
    else
    {
        cout<<"HasItem"<<endl;
    }
    cout<<m_Set.count('a')<<endl;


    for(int i=0;i<10;i++)
    {
        m_Set1.insert(i);
    }

    print(m_Set1);
    m_Set1.erase(5);
    print(m_Set1);


    set<int>::iterator iter;
    iter=m_Set1.find(5);
    if(iter!=m_Set1.end())
    {
        cout<<*iter<<endl;
    }
    else
    {
        cout<<" no such key"<<endl;
    }

    cout<<m_Set1.size()<<endl;

    for(int i=0;i<10;i++)
    {
        m_Set2.insert(i*2);
    }
    m_Set1.swap(m_Set2);
    print(m_Set1);
    print(m_Set2);

    cout<<m_Set1.max_size()<<endl;

    if(m_Set1!=m_Set2)cout<<"m_set1!=m_set2"<<endl;
    if(m_Set1==m_Set2)cout<<"m_set1==m_set2"<<endl;
    if(m_Set1<=m_Set2)cout<<"m_set1<=m_set2"<<endl;
    if(m_Set1<m_Set2)cout<<"m_set1<m_set2"<<endl;
    if(m_Set1>=m_Set2)cout<<"m_set1>=m_set2"<<endl;
    if(m_Set1>m_Set2)cout<<"m_set1>m_set2"<<endl;

    print1(m_Set1);


    iter=m_Set1.lower_bound(6);
    if(iter!=m_Set1.end())
    {
        cout<<"the first key >=6 is "<<*iter<<endl;
    }

    iter=m_Set1.upper_bound(6);
    if(iter!=m_Set1.end())
    {
        cout<<"the first key >6 is "<<*iter<<endl;
    }
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值