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 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 - 容器 - 运行期指定排序准则的更多相关文章
- STL - Map - 运行期自定义排序
RuntimeStringCmp.cpp #include <string> using namespace std; // function object to compare stri ...
- 运用map并于执行期指定排序准则
该例展示以下技巧: 如何使用map 如何撰写和使用仿函数 如何在执行期定义排序规则 如何在"不在乎大小写"的情况下比较字符串 #include<iostream> #i ...
- c++ set容器排序准则
转载两篇博客: http://blog.csdn.net/lishuhuakai/article/details/51404214 http://blog.csdn.net/lihao21/artic ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- C++关联式容器的排序准则
stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...
- STL容器——对map排序
STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- STL 查找vector容器中的指定对象:find()与find_if()算法
1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...
- STL容器的排序
STL容器的排序,支持随机访问的容器vector,deque,string没有sort成员,可调用std::sort排序:list排序调用自带的list::sort. 下面是std::sort函数,有 ...
随机推荐
- 【失踪人口回归】第11届东北地区大学生程序设计竞赛——Time to make some change
对哈尔滨出租车和纸质题目和2148473647的吐槽都被毕克神牛在知乎上(https://www.zhihu.com/question/59782275/answer/169402588)pick/b ...
- bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
4176: Lucas的数论 Time Limit: 30 Sec Memory Limit: 256 MB Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么 ...
- wikioi 1048 石子归并
dp[i][j]=min(dp[i][j],dp[i][k],dp[k+1][j]+sum[i][j]); 表示i-j的最小合并代价. #include <iostream> #inclu ...
- 读书笔记_Effective_C++_条款四十:明智而审慎地使用多重继承
多重继承是一种比较复杂的继承关系,它意味着如果用户想要使用这个类,那么就要对它的父类也了如指掌,所以在项目中会带来可读性的问题,一般我们都会尽量选择用单继承去替代它. 使用多重继承过程容易碰到的问题就 ...
- Git_搭建Git服务器
在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想 ...
- 忘记Oracle系统管理员sys的密码
sqlplus / as sysdba; alter user sys identified by newpwd***;
- 使用LM2576制作数控电源
http://www.daxia.com/bibis/moredata30_1207792_29862.shtml 图中DA和PWM任选其一, 当DA或PWM输出为0~1.25V时,输出在12.5V~ ...
- NAT(网络地址转换)
NAT(Network Address Translation,网络地址转换) 用途:当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因特网上的主机 ...
- D-U-N-S申请流程
1.打开D-U-N-S官网 http://fedgov.dnb.com/webform 图一 2.占击页面的“Click here to request your ......” (如图一红框所示)进 ...
- [PHP] ubuntu16.04下 Phpstorm发布项目到apache
reference to : http://blog.csdn.net/qq_23937195/article/details/72953308 在网上找的不靠谱,倒腾了大半天的,终于找到正确姿势QA ...