STL - Map - 运行期自定义排序
RuntimeStringCmp.cpp
#include <string> using namespace std; // function object to compare strings
// - allows you to set the comparison criterion at runtime
// - allows you to compare case insensitive
class RuntimeStringCmp
{
public:
// constants for the comparison criterion
enum cmp_mode { normal, nocase };
private:
// actual comparison mode
const cmp_mode mode; // auxiliary function to compare case insensitive
static bool nocase_compare(char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
public:
// constructor: initializes the comparison criterion
RuntimeStringCmp(cmp_mode m = normal) : mode(m) { } // the comparison
bool operator() (const string& s1, const string& s2) const
{
if (mode == normal)
{
return s1<s2;
}
else
{
return lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
nocase_compare);
}
}
};
MapAdvanceTest.h
#ifndef _Stl_Container_Map_Advance_Test_H_
#define _Stl_Container_Map_Advance_Test_H_ #include "../../TestBase.h"
#include <map> class RuntimeStringCmp;
typedef map<string, string, RuntimeStringCmp> StringStringMap; class MapAdvanceTest : public TestBase
{
public: MapAdvanceTest(const string &c, const string &d) : TestBase(c, d) { }
void run();
private:
...
void runtimeMapCompare();
// private inner method
void fillAndPrint(StringStringMap& coll);
}; #endif
MapAdvanceTest.cpp
#include <map>
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include "../../Core/RuntimeStringCmp.hpp"
#include "MapAdvanceTest.h"
#include "../../Core/ContainerUtil.h" using namespace std; ... void MapAdvanceTest::fillAndPrint(StringStringMap& coll)
{
// insert elements in random order
coll["Deutschland"] = "Germany";
coll["deutsch"] = "German";
coll["Haken"] = "snag";
coll["arbeiten"] = "work";
coll["Hund"] = "dog";
coll["gehen"] = "go";
coll["Unternehmen"] = "enterprise";
coll["unternehmen"] = "undertake";
coll["gehen"] = "walk";
coll["Bestatter"] = "undertaker"; // print elements
cout.setf(ios::left, ios::adjustfield);
for (const auto& elem : coll)
{
cout << setw() << elem.first << " "
<< elem.second << endl;
}
cout << endl;
cout << "#############################################" << endl;
} void MapAdvanceTest::runtimeMapCompare()
{
// create a container with the default comparison criterion
StringStringMap coll1;
fillAndPrint(coll1); // create an object for case-insensitive comparisons
RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase); // create a container with the case-insensitive comparisons criterion
StringStringMap coll2(ignorecase);
fillAndPrint(coll2);
} void MapAdvanceTest::run()
{
printStart("runtimeMapCompare()");
runtimeMapCompare();
printEnd("runtimeMapCompare()");
}
运行结果:
---------------- runtimeMapCompare(): Run Start ----------------
Bestatter undertaker
Deutschland Germany
Haken snag
Hund dog
Unternehmen enterprise
arbeiten work
deutsch German
gehen walk
unternehmen undertake
#############################################
arbeiten work
Bestatter undertaker
deutsch German
Deutschland Germany
gehen walk
Haken snag
Hund dog
Unternehmen undertake
#############################################
---------------- runtimeMapCompare(): Run End ----------------
STL - Map - 运行期自定义排序的更多相关文章
- STL - 容器 - 运行期指定排序准则
RuntimeCmp.hpp #include <set> using namespace std; // type for runtime sorting criterion class ...
- map的默认排序和自定义排序
STL的容器map为我们处理有序key-value形式数据提供了非常大的便利,由于内部红黑树结构的存储,查找的时间复杂度为O(log2N). 一般而言,使用map的时候直接采取map<typen ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- std::map 自定义排序
PS:开发中难免会用到快速检索的数据结构-map , 很多时候map自身提供的排序不能满足我们的需要或者不支持我们自定的数据结构的排序,解决办法就是自己实现排序. 这里的小案例是:我们要经用户的has ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase
今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0 这个程序使用了vect ...
- CCF CSP 201503-2 数字排序 (map+自定义排序)
题目链接:http://118.190.20.162/view.page?gpid=T26 返回试题列表 问题描述 试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: ...
- C++中vector,set,map自定义排序
一.vector排序 vector支持cmp,就类似数组,可以直接sort. #include <iostream> #include <algorithm> #include ...
- C++ STL中Map的按Key排序跟按Value排序
C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...
随机推荐
- Spring Boot 运作原理
Spring Boot 运作原理 1.Spring Boot 简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了 ...
- Android 打包出现jdk版本错误的问题
Android 打包出现 jdk 版本错误的问题,本质上是 SDK 的问题,与 JDK 无关.如果 SDK 的 API 是24或者更高,就要求 jdk 1.8,我这里指定的 API 是22,所以去勾选 ...
- django官方文档读书笔记
写在前面:这算是第二次读英文原文文档,第一次是读scrapy,感觉还是要做笔记,好记性不如烂笔头,现在已经忘了scrapy文档讲了什么了,心疼.以后要多读多写 经过半年的基础学习(懒,拖延)终于来到w ...
- win7 64位安装pywin32
先安装pywin32-218.win-amd64-py3.4.exe文件: 安装完成后,用CMD管理员模式进入\Python34\Scripts\目录 输入pywin32_postinstall.py ...
- php发送get、post请求的6种方法代码示例
本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...
- JDK环境变量的配置说明
一.Linux下的JDK安装及配置: 1. 使用命令uname –a查看系统的版本确定系统的位数,然后去JDK官网下载相应位数的安装程序,进行安装. 2. 使用rz命令将下载的JDK上传至Linux ...
- Java_基础知识回顾
1.ByteArrayInputStream. ByteArrayOutputStream String str = "ZHANGSAN"; //System.out.printl ...
- Git_版本回退
现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...
- run commands in linux shell using batch file
adb shell as root after device rooted once device rooted, we must perform "su" before we g ...
- 动态创建html元素的几种方法
可以通过以下几种方式动态创建html元素: 1.使用jQuery创建元素的语法 2.把动态内容存放到数组中,再遍历数组动态创建html元素 3.使用模版 □ 使用jQuery动态创建元素追加到jQ ...