该例展示以下技巧:

  • 如何使用map
  • 如何撰写和使用仿函数
  • 如何在执行期定义排序规则
  • 如何在“不在乎大小写”的情况下比较字符串
#include<iostream>
#include<map>
#include<algorithm>
#include<iomanip>
#include<string> using namespace std; class RuntimeStringCmp
{
public:
enum cmp_mode{normal,nocase}; private:
const cmp_mode mode; static bool nocase_compare(char c1, char c2)
{
return toupper(c1) < toupper(c2);
} public:
RuntimeStringCmp(cmp_mode m = normal) :mode(m){} 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);
}
}; typedef map<string, string, RuntimeStringCmp> StringStringMap;
void fillAndPrint(StringStringMap& coll); int main()
{
StringStringMap coll1;
fillAndPrint(coll1); RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase);
StringStringMap coll2(ignorecase);
fillAndPrint(coll2); system("pause");
return 0;
} void fillAndPrint(StringStringMap& coll)
{
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"; StringStringMap::iterator pos;
cout.setf(ios::left, ios::adjustfield);
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << setw(15) << pos->first.c_str() << " "
<< pos->second << endl;
}
cout << endl;
}

本例摘自:C++标准库P212

运用map并于执行期指定排序准则的更多相关文章

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

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

  2. c++ set容器排序准则

    转载两篇博客: http://blog.csdn.net/lishuhuakai/article/details/51404214 http://blog.csdn.net/lihao21/artic ...

  3. map集合的见解、排序

    map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等 HashMap:我们最常用的Map,它根据key的HashCode 值 ...

  4. C++关联式容器的排序准则

    stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...

  5. 浅析SQL查询语句未显式指定排序方式,无法保证同样的查询每次排序结果都一致的原因

    本文出处:http://www.cnblogs.com/wy123/p/6189100.html 标题有点拗口,来源于一个开发人员遇到的实际问题 先抛出问题:一个查询没有明确指定排序方式,那么,第二次 ...

  6. C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客

    C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客 C++中实现对map按照value值进行排序 2012-03-15 15:32:36 标签:map 职场 休闲 排 ...

  7. PHP 二维数组根据某个字段按指定排序方式排序

    /** * 二维数组根据某个字段按指定排序方式排序 * @param $arr array 二维数组 * @param $field string 指定字段 * @param int $sort_or ...

  8. sqlserver指定排序字段

    在sqlserver中可以指定排序的字段,需要将哪个字段值排在最前面或最后面,都是可以的.见如下代码: SELECT * FROM public_comment order by case [User ...

  9. C++ multiset通过greater、less指定排序方式,实现最大堆、最小堆功能

    STL中的set和multiset基于红黑树实现,默认排序为从小到大. 定义三个multiset实例,进行测试: multiset<int, greater<int>> gre ...

随机推荐

  1. Pycharm下tensorflow导入错误

    问题: ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory 解决方案: L ...

  2. SVN使用出现的问题及解决方案

    SVN使用出现的问题及解决方案 一.问题描述: 使用TortoiseSVN-1.9.5进行CheckOut时,出现报错信息如下:  Unable to connect to a repository ...

  3. 转载:二次指数平滑法求预测值的Java代码

    原文地址: http://blog.csdn.net/qustmeng/article/details/52186378?locationNum=4&fps=1 import java.uti ...

  4. 20155216 2016-2017-2 《Java程序设计》第五周学习总结

    20155216 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 使用try,catch,finally处理异常 JVM会尝试执行try区块中的程序代码,如果 ...

  5. POJ 3254 Corn Fields状态压缩DP

    下面有别人的题解报告,并且不止这一个状态压缩题的哦···· http://blog.csdn.net/accry/article/details/6607703 下面是我的代码,代码很挫,绝对有很大的 ...

  6. test20181017 B君的第二题

    题意 分析 考场50分 旁边的L君告诉我,求的就是非升子序列的个数,于是写了个树状数组. 但是\(\mod{2333} > 0\)还需要组合数中没有2333的倍数,所以实际上只得了\(a_i \ ...

  7. Vue2.x directive自定义指令

    directive自定义指令 除了默认设置的核心指令( v-model 和 v-show ),Vue 也允许注册自定义指令. 注意,在 Vue2.0 里面,代码复用的主要形式和抽象是组件——然而,有的 ...

  8. Linux内核电源管理综述

    资料:http://blog.csdn.net/bingqingsuimeng/article/category/1228414http://os.chinaunix.net/a2006/0519/1 ...

  9. Vault 0.10包含了web ui

    Vault 是一个很不错的访问控制,secret api key 管理工具 新的0.10 有好多新的功能的添加,最棒的是有一个web ui 了 包含的新特性如下: K/V Secrets Engine ...

  10. Monocular 集成harbor helm 仓库

      harbor 已经支持了helm 仓库(使用chartmuseum),Monocular 是一个不错的helm 仓库可视化工具 测试Monocular集成harbor 私服功能 使用docker- ...