c++中在执行期指定排序准则

 1 //定义排序准则的仿函数
 2 template<class T>
 3 class RuntimeCmp
 4 {
 5 public:
 6     enum cmp_mode { _normal, _reverse};
 7 public:
 8     RuntimeCmp(cmp_mode m = _normal) : mode(m){}
 9
10     bool operator() (const T &t1, const T &t2) const{ return mode == _normal ? t1 < t2 : t2 < t1;}//重载()操作符
11     bool operator== (const RuntimeCmp &rc){ return mode == rc.mode;}
12
13 private:
14     cmp_mode mode;
15 };
16
17 int main()
18 {
19     typedef set<int, RuntimeCmp<int> > IntSet;
20     IntSet coll1;
21     coll1.insert(4);
22     coll1.insert(7);
23     coll1.insert(5);
24     coll1.insert(1);
25     coll1.insert(6);
26     coll1.insert(2);
27     coll1.insert(5);
28     cout << "coll1: ";
29     copy(coll1.begin(), coll1.end(), ostream_iterator<int>(cout, " "));
30     cout << endl;
31
32     RuntimeCmp<int> reverse_order(RuntimeCmp<int>::_reverse);
33
34     IntSet coll2(reverse_order);
35     coll2.insert(4);
36     coll2.insert(7);
37     coll2.insert(5);
38     coll2.insert(1);
39     coll2.insert(6);
40     coll2.insert(2);
41     coll2.insert(5);
42
43     cout << "coll2: ";
44     copy(coll2.begin(), coll2.end(), ostream_iterator<int>(cout, " "));
45     cout << endl;
46
47     coll1 = coll2;
48     coll1.insert(3);
49     cout << "coll1: ";
50     copy(coll1.begin(), coll1.end(), ostream_iterator<int>(cout, " "));
51     cout << endl;
52
53     //测试coll1和coll2排序准则是否相同
54     if(coll1.value_comp() == coll2.value_comp())
55     {
56         cout << "coll1 and coll2 have same sorting criterion" << endl;
57     }
58     else
59     {
60          cout << "coll1 and coll2 have different sorting criterion" << endl;
61     }

程序输出:

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 same sorting criterion

注:赋值操作符同时也赋值了排序准则

详见c++标准程序库

时间: 2024-08-12 02:55:40

c++中在执行期指定排序准则的相关文章

运用map,string并于执行期指定排序准则

class RuntimeStringCmp { public: enum cmp_mode { normal, nocase, }; RuntimeStringCmp(cmp_mode mod=normal):mode(mod) { } ~RuntimeStringCmp() { } static bool nocase_compare(char char1,char char2) { return toupper(char1) < toupper(char2); } bool operato

STL - 容器 - 运行期指定排序准则

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 v

qsort 函数的使用——对普通数组、指针数组、二维数组中的元素进行排序

在ANSI C中,qsort函数的原型是 #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *)); 解释:qsort函数对含有nmemb个元素的数组进行排序,而base指针指向数组的第一个元素.这个数组的元素个数由size指定. compar函数对qsort的比较操作进行定义,所以可以定制数字的比较,字符串的比较,甚至结构体

关联容器执行器指定排序规则

// // main.cpp // setcmp // // Created by IDM-PKU on 14-9-6. // Copyright (c) 2014年 PKU. All rights reserved. // #include <iostream> #include <set> #include "print.hpp" using namespace std; template <class T> class RuntimeCmp{

具体解释Redis源代码中的部分高速排序算法(pqsort.c)

看标题.你可能会疑惑:咦?你这家伙.怎么不解说完整的快排,仅仅讲一部分快排---.- 哎,冤枉. "部分快排"是算法的名字.实际上本文相当具体呢.本文差点儿与普通快排无异.看懂了本文,你对普通的快排也会有更深的认识了. 高速排序算法(qsort)的原理我们大都应该了解.本文介绍的是部分高速排序算法. 事实上其算法本质是一样的,仅仅只是限定了排序的左右区间.也就是仅仅对一个数字序列的一部分进行排序.故称为"部分高速排序算法".简称:pqsort Redis项目中的pq

浅析SQL查询语句未显式指定排序方式,无法保证同样的查询每次排序结果都一致的原因

本文出处:http://www.cnblogs.com/wy123/p/6189100.html 标题有点拗口, 先抛出问题:一个查询没有明确指定排序方式,那么,第二次执行这个同样的查询的时候,查询结果会不会与第一次的查询结果排序方式完全一样? 答案是不确定的,两个完全一样的查询,结果也完全一样,两次(多次)查询结果的排序方式有可能一致,有可能不一致. 如果不一致,又是什么原因导致同样的查询默认排序方式不一致? 以下简单分析几种情况,说明为什么查询同样的查询会出现默认排序结果不一样的情况.当然对

Oracle中针对中文进行排序[Z]

在oracle 9i之前,对中文的排序,是默认按2进制编码来进行排序的. 9i时增加了几种新的选择: 按中文拼音进行排序:SCHINESE_PINYIN_M 按中文部首进行排序:SCHINESE_RADICAL_M 按中文笔画进行排序:SCHINESE_STROKE_M 而oracle 9i是对中文的排序是默认按拼音排序(并不是指NLS_SORT = SCHINESE_PINYIN_M,而是说SQL中不指定NLS_SORT时对中文列排序时默认按拼音)的,跟之前的2进制编码排序有所不同.具体用法如

结合手机上网流量业务来说明Hadoop中的二次排序机制,分区机制

本篇博客将结合手机上网流量业务来详细介绍Hadoop的二次排序机制.分区机制,先介绍一下业务场景: 先介绍一下业务场景:统计每个用户的上行流量和,下行流量和,以及总流量和. 本次描述所用数据: 日志格式描述: 日志flowdata.txt中的具体数据: 首先我们先通过mapreduce程序实现上面的业务逻辑: 代码实现: package FlowSum; import java.io.DataInput; import java.io.DataOutput; import java.io.IOE

134在单元格中自动排列指定的数据

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataList; 5 @property (strong, nonatomic) NSMutableArray *mArrSortedCollation; 6 7 @end ViewCo