(转)c++中std::set自定义去重和排序函数 - 南宫轩诺 - 博客园

本文转自:c++中std::set自定义去重和排序函数 - 南宫轩诺 - 博客园
#include < set> using namespace std; struct song { int m_id; int m_hot; song( int id, int hot) { this->m_id = id; this->m_hot = hot; } bool operator<( const struct song & right) const // 重载<运算符 { if( this->m_id == right.m_id) // 根据id去重 return false ; else { if( this->m_hot != right.m_hot) { return this->m_hot > right.m_hot; // 降序 } else { return this->m_id > right.m_id; } } } }; void main() { std:: set<song> mySet; song s1( 10, 100 ); song s2( 20, 200 ); song s3( 20, 300 ); song s4( 30, 200 ); mySet.insert(s1); // 插入s1 mySet.insert(s2); // 插入s2 mySet.insert(s3); // s3和s2的id相同,不插入 mySet.insert(s4); // 插入s4 for (auto it:mySet) { std::cout<< ” id: ”<<it.m_id<< ” ,hot: ”<<it.m_hot<< std::endl; } std::cout<< ” end ”<< std::endl; };
结果如下:
id:30,hot : 200
id:20,hot : 200
id:10,hot : 100
end

         2.2 重载()运算符

     具体代码如下:

 1 #include <iostream>
 2 #include <set>
 3 using namespace std;
 4 struct song
 5 {
 6     int m_id;
 7     int m_hot;
 8     song(int id,int hot)
 9     {
10 
11         this->m_id = id;
12         this->m_hot = hot;
13     }
14     /*
15     bool operator<(const struct song & right)const   //重载<运算符
16     {
17         if(this->m_id == right.m_id)     //根据id去重
18             return false;
19         else
20         {
21             if(this->m_hot != right.m_hot)
22             {
23                 return this->m_hot > right.m_hot;      //降序
24             }
25             else
26             {
27                 return this->m_id > right.m_id;     
28             }
29         }
30     }
31     */
32 };
33 struct comp   
34 {
35     bool operator()(struct song left,struct song  right)  //重载()运算符
36     {
37 
38         if(left.m_id == right.m_id)     //根据id去重
39             return false;
40         else
41         {
42             if(left.m_hot != right.m_hot)
43             {
44                 return left.m_hot > right.m_hot;      //降序
45             }
46             else
47             {
48                 return left.m_id > right.m_id;     
49             }
50 
51         }
52     }
53 
54 };
55 void main()
56 {
57     std::set<song,comp> mySet;      //写法和2.1中的的区别
58     song s1(10,100);
59     song s2(20,200);
60     song s3(20,300);
61     song s4(30,200);
62     mySet.insert(s1);    //插入s1
63     mySet.insert(s2);    //插入s2
64     mySet.insert(s3);    //s3和s2的id相同,不插入
65     mySet.insert(s4);    //插入s4
66     for(auto it:mySet)
67     {
68         std::cout<<"id:"<<it.m_id<<",hot:"<<it.m_hot<<std::endl;
69     }
70     std::cout<<"end"<<std::endl;
71 };
结果如下:
id:30,hot : 200
id:20,hot : 200 id:10,hot : 100
end

    

 

    </div>
    <div class = "postDesc">posted @ <span id="post-date">2017-04-04 17:39</span> <a href='https://www.cnblogs.com/litaozijin/'>南宫轩诺</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=6665595" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(6665595);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=330597,cb_entryId=6665595,cb_blogApp=currentBlogApp,cb_blogUserGuid='2e72332b-4aec-e611-845c-ac853d9f53ac',cb_entryCreatedDate='2017/4/4 17:39:00';loadViewCount(cb_entryId);var cb_postType=1;</script>


fixPostBody(); setTimeout(function () { incrementViewCount(cb_entryId); }, 50); deliverAdT2(); deliverAdC1(); deliverAdC2(); loadNewsAndKb(); loadBlogSignature(); LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid); GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType); loadOptUnderPost(); GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
</div><!--end: forFlow -->
</div><!--end: mainContent 主体内容容器-->

<div id="sideBar">
    <div id="sideBarMain">

公告

        <div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>

        <div id="leftcontentcontainer">
            <div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
        </div>

    </div><!--end: sideBarMain -->
</div><!--end: sideBar 侧边栏容器 -->
<div class="clear"></div>
</div><!--end: main -->
<div class="clear"></div>
<div id="footer">


Copyright ©2018 南宫轩诺



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值