加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++ STL Set 快速入门

发布时间:2020-12-16 07:47:33 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 set是一个集合,其中元素有序,排序的方式按照指定的方式来排序,不指定则默认按照升序排列 set中元素不可以相同;比较两个set相同,他们的排序方式和

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

set是一个集合,其中元素有序,排序的方式按照指定的方式来排序,不指定则默认按照升序排列
set中元素不可以相同;比较两个set相同,他们的排序方式和元素都要相同;不能直接改变元素的值,需要先删除,再插入。
    #include <iostream>  
    #include <iterator>  
    #include <algorithm>  
    #include <set>  
    using namespace std;  
    struct classcomp     
    {    
        bool operator()(const char& lhs,const char& rhs)    
        {    
            return lhs > rhs;    
        }    
    };   
    char array[] = {'e','f','g'};  
    int _tmain(int argc,_TCHAR* argv[])  
    {  
        set<char,classcomp> myset;  
        myset.insert('a');  
        myset.insert('b');  
        myset.insert('c');  
        copy(myset.begin(),myset.end(),ostream_iterator<char>(cout," "));  
        cout<<endl;  
        cout<<myset.insert('d').second<<endl;  
        //Now a b c d  
          
        myset.insert(array,array+3);  
        copy(myset.begin()," "));  
        cout<<endl;  
        //Now g f e d c b a  
        myset.erase('d');  
        //Now g f e c b a  
        myset.erase(myset.begin());  
        //Now f e c b a   
        copy(myset.begin()," "));  
        cout<<endl;  
        //myset.clear();//清空set  
        set<char,classcomp>::iterator it= myset.begin();  
        if ((it = myset.find('k')) == myset.end())  
        {  
            cout<<"Can not find K in this set"<<endl;  
        }  
        cout<<"Current size is :"<<myset.size()<<endl;  
    }  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读