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 - 运行期自定义排序的更多相关文章

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

    RuntimeCmp.hpp #include <set> using namespace std; // type for runtime sorting criterion class ...

  2. map的默认排序和自定义排序

    STL的容器map为我们处理有序key-value形式数据提供了非常大的便利,由于内部红黑树结构的存储,查找的时间复杂度为O(log2N). 一般而言,使用map的时候直接采取map<typen ...

  3. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  4. std::map 自定义排序

    PS:开发中难免会用到快速检索的数据结构-map , 很多时候map自身提供的排序不能满足我们的需要或者不支持我们自定的数据结构的排序,解决办法就是自己实现排序. 这里的小案例是:我们要经用户的has ...

  5. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

  6. STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase

    今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0         这个程序使用了vect ...

  7. CCF CSP 201503-2 数字排序 (map+自定义排序)

    题目链接:http://118.190.20.162/view.page?gpid=T26 返回试题列表 问题描述 试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: ...

  8. C++中vector,set,map自定义排序

    一.vector排序 vector支持cmp,就类似数组,可以直接sort. #include <iostream> #include <algorithm> #include ...

  9. C++ STL中Map的按Key排序跟按Value排序

    C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...

随机推荐

  1. dos系统下的游戏~ 不断更新中

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

  2. ASP.NET MVC 手机短信验证

    本文来自于stoneniqiu的文章,原文地址 http://www.cnblogs.com/stoneniqiu/p/6234002.html 1.注册一个应用 得到AppKey 和 App Sec ...

  3. Windows下Python版本的切换

    通常在Windows系统下我们可能安装了多个Python版本,那么该如何进行版本的切换呢?下面就Python2.7与Python3.0版本进行简单说明. 1.首先需要在Windows上安装Python ...

  4. 使用Layui和Vue实现分页

    原理就是利用Layui的分页组件和Vue组件的模板渲染功能. 我下面直接贴代码,比较直观. index.html <!DOCTYPE html> <html> <head ...

  5. elasticsearch中文分词+全文搜索demo

    本文假设你已经搭建好elasticsearch服务器,并在上面装了kibana和IK中文分词组件 elasticsearch+kibana+ik的安装,之前的文章有介绍,可参考. mapping介绍: ...

  6. hdu 1847 博弈基础题 SG函数 或者规律2种方法

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. GIT 详解2

    https://segmentfault.com/a/1190000000738398 http://www.cnblogs.com/cposture/p/4903767.html https://g ...

  8. erlang debug

    http://www.cnblogs.com/goldli/archive/2011/03/08/1976998.html erlang 模块文件 helloworld.erl 文件内容: -modu ...

  9. linux系统编程:线程原语

    线程原语 线程概念 线程(thread),有时被称为轻量级进程(Lightweight Process,LWP).是程序运行流的最小单元.一个标准的线程由线程ID.当前指令指针(PC),寄存器集合和堆 ...

  10. Android Binder分析二:Natvie Service的注冊

    这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向Servi ...