博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL - Map - 运行期自定义排序
阅读量:6516 次
发布时间:2019-06-24

本文共 3277 字,大约阅读时间需要 10 分钟。

RuntimeStringCmp.cpp

#include 
using namespace std;// function object to compare strings// - allows you to set the comparison criterion at runtime// - allows you to compare case insensitiveclass 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

MapAdvanceTest.h

#ifndef        _Stl_Container_Map_Advance_Test_H_#define        _Stl_Container_Map_Advance_Test_H_#include "../../TestBase.h"#include class RuntimeStringCmp;typedef map
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 #include 
#include
#include
#include
#include
#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(15) << 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 ----------------

 

转载于:https://www.cnblogs.com/davidgu/p/4974020.html

你可能感兴趣的文章
细说C#:委托的简化语法,聊聊匿名方法和闭包(上)
查看>>
Elixir Ecto: 使用 ExMachina 批量生成测试数据
查看>>
虚拟机类加载机制(读书笔记)
查看>>
PHP学习计划
查看>>
[稀土掘金日报] JavaScript 开发者必备的资源合集
查看>>
Win软件私家珍藏-常用软件工具使用总结
查看>>
iToolkit,推荐我们自己做的一套前端组件
查看>>
Junit源码阅读(一)
查看>>
JavaScript设计模式与开发实践 | 01 - 面向对象的JavaScript
查看>>
捷信达酒店管理系统使用说明
查看>>
使用java api 创建excel内容并发送邮件
查看>>
Unity3d删除无用的美术资源轻量级插件
查看>>
2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
查看>>
Linux基础命令---umask
查看>>
Nginx 性能优化(学习笔记二十五)
查看>>
Strategy for Python Challenge(01)
查看>>
Spring事务异常回滚,try catch 捕获异常不回滚
查看>>
钢管识别项目1
查看>>
iOS的@try、@catch()
查看>>
中科院院士谭铁牛:人工智能发展需要理性务实
查看>>