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.假如存储学生和其成绩(假定 ...
随机推荐
- bzoj 1492
这道题真好... 首先,感觉像DP,但是如果按照原题意,有无数个状态,每个状态又有无数个转移. 然后思考,我们每次买一部分和卖一部分的原因是什么,如果没有那个比例(就是rate=1恒成立),那么很容易 ...
- word-ladder总结
title: word ladder总结 categories: LeetCode tags: 算法 LeetCode comments: true date: 2016-10-16 09:42:30 ...
- Codeforces Round #355 (Div. 2) C. Vanya and Label 水题
C. Vanya and Label 题目连接: http://www.codeforces.com/contest/677/problem/C Description While walking d ...
- 连接本地websocket服务延迟的问题
今天用C#编写了一个Chrome Remote Debugger的客户端程序,发现使用rest和websocket程序时第一次连接的时候特别慢,大概每次都要消耗一秒左右,而用chrome直接连接却没有 ...
- systemtap 2.8 安装说明书
systemtap: a linux trace/probe tool Visit the project web site at <http://sourceware.org/systemta ...
- java string常见操作(二)
- ios的一些知识点
ios的一些知识点 一 非ARC的内存管理情况 1-autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段落,开 ...
- 单独配置secondarynamenode
一.配置说明 这是在我之前yarn框架上通过加入节点,改动相关的配置文件,使得secondarynamenode独立出来的,所以这里前期的一系列琐碎配置请參考我之前的博客: http://blog.c ...
- 汇编语言的Hello World
汇编语言的Hello World,汇编语言没有我想象的那么低级,它已经具备了不少高级语言的特性,代码也变得清晰,层次清楚,易于维护了. a.asm .386 .model flat,stdcall ...
- 在VS 2010上搭建Windows Phone 7开发平台
如今Windows Phone 7平台越来越火了,刚刚拿到一款新的Windows Phone,于是准备在电脑上搭建WP7的开发环境. 首先,安装VS2010,升级到SP1,并安装Windows P ...