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函数,有 ...
随机推荐
- 算法转AI平台工程师记录-0
--- vim源码安装: 1. git clone https://github.com/vim/vim.git 2. cd vim && ./configure --prefix=x ...
- Codeforces Round #296 (Div. 1) B. Clique Problem 贪心
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 电子助视仪 对比增强算法 二十种色彩模式(Electronic Video Magnifier, 20 color mode)
电子助视仪 是一种将原始彩色图像转换为某种对比度高的图像,例如将原始图像变换为黑底白字,红底白字,白底红字,蓝底黄字,黄字蓝底等等.电子助视仪的主要应用场景为为老人或者特殊弱视人群的阅读.国内国外均有 ...
- PHP个人博客系统开发历程
声明: Author:GenialX GenialX's QQ:2252065614 GenialX's URL:胡旭博客 - www.ihuxu.com 一年多曾经的某一天,我在上交实验报告时,偶然 ...
- python性能优化建议
参考: https://segmentfault.com/a/1190000000666603 http://blog.csdn.net/zhoudaxia/article/details/23853 ...
- 报错:不允许保存更改。您所做的更改要求删除并重新创建以下表……
在使用SQL Server 2008为某个表添加列的时候出现启用了"阻止保存要求重新创建表的更改问题的设置方法..."报错: 解决方法: 工具--选项--Designers-- ...
- 【docker】关于docker中挂载的解释
现在有这么一个命令: docker run -p 33061:3306 --name mysql --restart=always -e MYSQL_ROOT_PASSWORD=pisen -v /e ...
- .NET:何时应该 “包装异常”?
背景 提到异常,我们会想到:抛出异常.异常恢复.资源清理.吞掉异常.重新抛出异常.替换异常.包装异常.本文想谈谈 “包装异常”,主要针对这个问题:何时应该 “包装异常”? “包装异常” 的技术形式 包 ...
- C#常使用的正则表达式
/// <summary> /// 是否为手机号码 /// </summary> /// <param name="value"></pa ...
- http状态码的含义及502, 503和504的区别
https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81#5xx%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%94%9 ...