RuntimeCmp.hpp

#include <set>

using namespace std;

// type for runtime sorting criterion
class RuntimeCmp
{
public:
enum cmp_mode { normal, reverse };
private:
cmp_mode mode;
public:
// constructor for sorting criterion
// - default criterion uses value normal
RuntimeCmp(cmp_mode m = normal) : mode(m) { }
// comparison of elements
// - member function for any element type
template <typename T>
bool operator() (const T& t1, const T& t2) const
{
return mode == normal ? t1<t2
: t2<t1;
}
// comparison of sorting criteria
bool operator== (const RuntimeCmp& rc) const
{
return mode == rc.mode;
}
}; // type of a set that uses this sorting criterion
typedef set<int, RuntimeCmp> IntSet;
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <functional>
#include "SetTest.h"
#include "../../Core/RuntimeCmp.hpp"
#include "../../Core/print.hpp" using namespace std; void SetTest::runtimeCompare()
{
// create, fill, and print set with normal element order
// - uses default sorting criterion
IntSet coll1 = { , , , , , , };
PRINT_ELEMENTS(coll1, "coll1: "); // create sorting criterion with reverse element order
RuntimeCmp reverse_order(RuntimeCmp::reverse); // create, fill, and print set with reverse element order
IntSet coll2(reverse_order);
coll2 = { , , , , , , };
PRINT_ELEMENTS(coll2, "coll2: "); // assign elements AND sorting criterion
coll1 = coll2;
coll1.insert();
PRINT_ELEMENTS(coll1, "coll1: "); // just to make sure...
if (coll1.value_comp() == coll2.value_comp())
{
cout << "coll1 and coll2 have the same sorting criterion"
<< endl;
}
else
{
cout << "coll1 and coll2 have a different sorting criterion"
<< endl;
}
} void SetTest::run()
{
printStart("runtimeCompare()");
runtimeCompare();
printEnd("runtimeCompare()");
}

运行结果:

---------------- runtimeCompare(): Run Start ----------------
coll1: 1 2 4 5 6 7
coll2: 7 6 5 4 2 1
coll1: 7 6 5 4 3 2 1
coll1 and coll2 have the same sorting criterion
---------------- runtimeCompare(): Run End ----------------

STL - 容器 - 运行期指定排序准则的更多相关文章

  1. STL - Map - 运行期自定义排序

    RuntimeStringCmp.cpp #include <string> using namespace std; // function object to compare stri ...

  2. 运用map并于执行期指定排序准则

    该例展示以下技巧: 如何使用map 如何撰写和使用仿函数 如何在执行期定义排序规则 如何在"不在乎大小写"的情况下比较字符串 #include<iostream> #i ...

  3. c++ set容器排序准则

    转载两篇博客: http://blog.csdn.net/lishuhuakai/article/details/51404214 http://blog.csdn.net/lihao21/artic ...

  4. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  5. C++关联式容器的排序准则

    stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...

  6. STL容器——对map排序

    STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...

  7. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  8. STL 查找vector容器中的指定对象:find()与find_if()算法

    1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...

  9. STL容器的排序

    STL容器的排序,支持随机访问的容器vector,deque,string没有sort成员,可调用std::sort排序:list排序调用自带的list::sort. 下面是std::sort函数,有 ...

随机推荐

  1. 【10.7校内测试】【队列滑窗】【2-sat】【贪心+栈二分+线段树(noip模拟好题)】【生日祭!】

    比较好想的一道题,直接用队列滑窗,因为扫一遍往队列里加东西时,改变的只有一个值,开桶储存好就行了! #include<bits/stdc++.h> using namespace std; ...

  2. bzoj 3545/3551: [ONTAK2010]Peaks -- 主席树,最小生成树,倍增

    3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MB Description 在Bytemountains有N座山峰,每座山峰 ...

  3. 事件冒泡 比bubble

    冒泡的概念就是 当子元素触发事件的时候 相应的祖宗十八代素也会触发相同的事件(前提父元素也添加了一样的事件)eg:儿子 有一个onclick 祖宗十八代 也有onclick 当点击儿子的时候 祖宗十八 ...

  4. IOS7开发~Images.xcassets

    from:http://blog.csdn.net/liufan321/article/details/9121241 新建项目,如下所示: 本文分享一下Images.xcassets的体验~_~ 1 ...

  5. QCon大会上推荐阅读的10本书

    QCon北京2014大会将于4月25-27日在北京国际会议中心盛大开幕.QCon是由@InfoQ 主办的全球顶级技术盛会.在此次盛会中,现场将有@人民邮电出版社-信息技术分社 主办的现场扫描二维码赠书 ...

  6. 改变 Windows 用户文件夹默认位置

    https://social.microsoft.com/Forums/zh-CN/b6602d50-1a75-42d2-90bd-3dc6c2c82147/programdata?forum=win ...

  7. HUST 1017 Exact cover(DLX精确覆盖)

    Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...

  8. java基础知识概要图

  9. 【centOS】centos7 查看和关闭防火墙

    查看防火墙状态 firewall-cmd --state running代表防火墙正在运行 停止firewall systemctl stop firewalld.service 禁止firewall ...

  10. wcf配置参数说明

    Open/Close/Receive/Send本是HTTP/TCP/SOCKET的概念,Read/Write Operation则是Web Service的概念. 1.OpenTimeout 客户端与 ...