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. bzoj2243 染色

    Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如 ...

  2. bmp图像数据转jpg图像的C语言实现

    bmp图像格式协议 jpg图像格式协议 Bmp转jpg图像代码 密码:nmaj

  3. 关于Vue的一些小技巧

    前言 用Vue开发一个网页并不难,但是也经常会遇到一些问题,其实大部分的问题都在文档中有所提及,再不然我们通过谷歌也能成功搜索到问题的答案,为了帮助小伙伴们提前踩坑,在遇到问题的时候,心里大概有个谱知 ...

  4. Tasker to create toggle widget for ES ftp service -- Send Intent

    To perform this mission, Tap the tab "tasks" of Tasker, create a task as below.Task: (ES F ...

  5. jQuery碎语(2) 事件

    4.事件 ● 通过方法名给元素绑定事件: $('li').click(function(event){}) ● 通过bind方法给元素绑定事件: $('li') .bind('click',funct ...

  6. NativeXml

    NativeXml GITHUB: https://github.com/kattunga/NativeXml THIS IS A FORK WITH SOME FIXES AND IMPROVEME ...

  7. 什么是软件project?

    Normal 0 7.8 pt 0 2 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNo ...

  8. iOS动画相关(持续更新)

    1.When my application is entering background, because the user push the home button, the animations ...

  9. C语言变量的类型和存储位置

    . C语言变量主要分为全局变量.静态全局变量.局部变量.静态局部变量和寄存器变量.其中静态变量用static关键字进行修饰.程序所占用的内存可以分为以下几个部分: ()代码段-存放程序代码,只读的,不 ...

  10. 【TYVJ 五月图论专项有奖比赛】

    最短路+TSP+最小生成树+倍增LCA+TreeDP 第一题 其实是个TSP问题(然而我没发现),但是关键点很少,只有5个,所以用dij+heap分别预处理出来这五个点为源的最短路…… 然后枚举起点 ...