运用map并于执行期指定排序准则
该例展示以下技巧:
- 如何使用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并于执行期指定排序准则的更多相关文章
- STL - 容器 - 运行期指定排序准则
RuntimeCmp.hpp #include <set> using namespace std; // type for runtime sorting criterion class ...
- c++ set容器排序准则
转载两篇博客: http://blog.csdn.net/lishuhuakai/article/details/51404214 http://blog.csdn.net/lihao21/artic ...
- map集合的见解、排序
map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等 HashMap:我们最常用的Map,它根据key的HashCode 值 ...
- C++关联式容器的排序准则
stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...
- 浅析SQL查询语句未显式指定排序方式,无法保证同样的查询每次排序结果都一致的原因
本文出处:http://www.cnblogs.com/wy123/p/6189100.html 标题有点拗口,来源于一个开发人员遇到的实际问题 先抛出问题:一个查询没有明确指定排序方式,那么,第二次 ...
- C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客
C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客 C++中实现对map按照value值进行排序 2012-03-15 15:32:36 标签:map 职场 休闲 排 ...
- PHP 二维数组根据某个字段按指定排序方式排序
/** * 二维数组根据某个字段按指定排序方式排序 * @param $arr array 二维数组 * @param $field string 指定字段 * @param int $sort_or ...
- sqlserver指定排序字段
在sqlserver中可以指定排序的字段,需要将哪个字段值排在最前面或最后面,都是可以的.见如下代码: SELECT * FROM public_comment order by case [User ...
- C++ multiset通过greater、less指定排序方式,实现最大堆、最小堆功能
STL中的set和multiset基于红黑树实现,默认排序为从小到大. 定义三个multiset实例,进行测试: multiset<int, greater<int>> gre ...
随机推荐
- kbmMW CopyRawRecords 用法
复制一个ClientQuery数据集到另外一个ClientQuery,我们应该怎么做?并注意什么呢? kbmMW为我们提供了好几个方法,有LoadFromDataSet,CopyRawRecords, ...
- Nexus私服的安装与配置
Nexus的安装与配置 仅以此文,献给陷入懒癌晚期的小伙伴们. 本文基于nexus 3.xx .0. What?Why?When?Who?Where? Sonatype Nexus是一款maven仓库 ...
- Pandas 命令整理
在网上看到一个整理的很好的pandas命令合集,转一份供自己查找与参考
- webbench-1.5_hacking
/**************************************************************************** * * webbench-1.5_hacki ...
- 【OpenCV】Learn OpenCV
learn opencv website: https://www.learnopencv.com/ learn opencv github:https://github.com/spmallick/ ...
- 本地和服务器(ubuntu)文件同步
秘钥登录远端服务器 rsync -avze 'ssh -i ./id_rsa' root@remoteIp:/xx/remotefile.txt ./localpath (./id_rsa为本地秘钥路 ...
- ElasticSearch(八):springboot集成ElasticSearch集群并使用
1. 集群的搭建 见:ElasticSearch(七) 2. springboot配置集群 2.1 创建springboot项目,使用idea创建,不过多介绍(创建项目时候建议不要勾选elastics ...
- springboot项目搭建
https://blog.csdn.net/u012702547/article/details/54319508
- benthos stream 处理工具说明
benthos 是golang 编写的流处理工具,同时也可以作为一个类库使用,当前支持的source sink 还是比较全的 (kafka rabbitmq http service s3 redis ...
- Microsoft OWIN
About OWIN defines a standard interface between .NET web servers and web applications. The goal of t ...