(转)c++ set容器排序准则

转自:c++ set容器排序准则 - 每天一点积累 - 博客园

c++ set容器排序准则

转载两篇博客:

http://blog.csdn.net/lishuhuakai/article/details/51404214

http://blog.csdn.net/lihao21/article/details/6302196/

以下是实验代码:

#include <iostream>  




#include < set> #include<algorithm> using namespace std; /* Student结构体 */ struct Student { string name; int id; int score; }; /* “仿函数”。为Student set指定排序准则 */ class studentSortCriterion { public :
       /*类型要与set容器类型一致*/
bool operator() ( const Student *a, const Student *b) const { return (a->id == b->id) ? (a->score > b->score) :(a->id > b-> id); } }; int main() { set<Student*, studentSortCriterion> stuSet; set<Student*> stus; Student stu1, stu2,stu3; stu1.name = 张三 ; stu1.id = 2 ; stu1.score = 100 ; stu2.name = 李四 ; stu2.id = 1 ; stu2.score = 90 ; stu3.name = 小明 ; stu3.id = 3 ; stu3.score = 80 ; stuSet.insert(& stu1); stuSet.insert(& stu2); stuSet.insert(& stu3); Student stuTem; stuTem.score = 80 ; stuTem.id = 3 ; Student* stuTempPtr; set<Student*, studentSortCriterion> ::iterator iter; iter = stuSet.find(& stuTem); if(iter != stuSet.end()) {   cout << (*iter)->name << endl; }
   else {   cout << Cannot find the student! << endl; } for(std:: set<Student*,studentSortCriterion>::iterator it = stuSet.begin();it!=stuSet.end();it++ ){ std::cout<<(*it)->name<< endl; } }

上面程序会根据学生ID先进行排名然后再根据分数进行排名,排序准则需要满足以下要求,摘自C++标准库第二版:

输出结果:

小明
小明
张三
李四

    </div>
    <div class = "postDesc">posted @ <span id="post-date">2017-04-12 17:01</span> <a href='https://www.cnblogs.com/hong2016/'>每天一点积累</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=6700146" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(6700146);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=344334,cb_entryId=6700146,cb_blogApp=currentBlogApp,cb_blogUserGuid='b1e14f24-6713-e711-845c-ac853d9f53ac',cb_entryCreatedDate='2017/4/12 17:01: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 每天一点积累



猜你喜欢

转载自blog.csdn.net/weixin_42993054/article/details/81984619
今日推荐