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. 2016 UESTC DP专题题解

    题解下载地址:http://pan.baidu.com/s/1eSx27Jk 题解下载地址:http://pan.baidu.com/s/1qYDxlhi

  2. Loadrunner问题:Monitor name :Windows Resources. Cannot create measurement Processor|% Processor Time|_Total on machine 192.168.0.1

    说明: 在Loadrunner监控windows系统资源的时候,在添加好windows Resources后运行发现报如下错误: int: Check that there is such a mea ...

  3. 恢复二进制文件中的block符号表

    前篇博客中,使用 杨君的小黑屋 提供的工具恢复二进制文件的符号表,只恢复了函数的符号表,本篇讲述如何恢复block符号表,杨君的博客中使用IDA分析二进制文件,本篇则使用MacOS系统上体验也不错的H ...

  4. MySQL内核整理(一)

    一.在共享表空间(系统表空间)中,innodb会维护一些系统信息:1.Internal data dictionary2.Rollback segments3.undo space4.insert b ...

  5. HDU 4718 The LCIS on the Tree (动态树LCT)

    The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  6. 利用.bat文件快速设置IE代理与清除IE代理

    http://www.duoluodeyu.com/2009/17.html 设置IE代理.bat文件原文:将下面红色文字复制保存为.bat文件即可. 复制后将蓝色字体部分改成你要设置的代理服务器地址 ...

  7. 报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>"

    报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>&q ...

  8. restful api安全验证问题

    没有绝对的安全,这个话题很深, 下文都是自己的一些理解,水平有限,如有勘误,希望大家予以指正. 由于 RESTful Api 是基于 Http 协议的 Api,是无状态传输,所以 只要和用户身份有关的 ...

  9. 转 iOS开发debug跟release版本log屏蔽方法

    简单介绍以下几个宏: ) __VA_ARGS__ 是一个可变参数的宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持).宏前面加上##的作用在于,当可变参数 ...

  10. 在使用完全拷贝过来的类文件(带xib文件)时,要及时修改 File's Owner