运用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 TkbmMWHashSHA256与Delphi 10.2 THashSHA2算出相同的结果
procedure TForm2.Button3Click(Sender: TObject); var hashed:string; begin hashed:=TkbmMWHashSHA256.Ha ...
- CentOS安装setup
如果在命令行运行 setup 提示乜有安装该命令,那么就需要先安装一下setup命令 执行:yum install setuptool #可以发现执行setup后不全,再安装一个用于系统服务管理yum ...
- Bootstrap CustomBox 弹层
这个模态窗口插件使用原生javascript制作,它也可以和jQuery完美的结合.请注意:这些模态窗口动画仅仅工作在支持各自CSS3属性的浏览器上.Internet Explorer 8 和 9需要 ...
- 微信小程序代码片段
微信小程序代码片段是一种可分享的小项目,可用于分享小程序和小游戏的开发经验.展示组件和 API 的使用.复现开发问题等等.分享代码片段会得到一个链接,所有拥有此分享链接的人可以在工具中导入此代码片段. ...
- Linux下常用的ftp操作命令
Linux下常用的ftp操作命令 =========== 完美的分割线 ============= 1.登陆ftp服务器 ftp [IP] [PORT] # 登陆ftp服务器,本机登陆可以不写IP 实 ...
- int类型转string类型c++
前言 使用VS的过程中,经常会用到需要将int类型数据转换为字符串类型,便于显示信息等. 实现方法 c++11标准中的to_string函数,在VS安装文件的include文件中生成的只读文件,使用起 ...
- 《DSP using MATLAB》Problem 4.14
代码: %% ---------------------------------------------------------------------------- %% Output Info a ...
- hdu1243 dp (类最长公共子序列)
题意:射击演习中,已知敌人出现的种类顺序,以及自己的子弹种类顺序,当同种类的子弹打到同种类的敌人时会得到相应分数,问最多能得多少分. 这题的题意很好理解,而且模型也很常见,是带权值的类最长公共子序列问 ...
- (转)C++初始化与赋值
来源:http://www.cnblogs.com/chio/archive/2008/10/06/1305145.html 先来个区别说明:赋值操作是在两个已经存在的对象间进行的,而初始化是要创建一 ...
- ZH奶酪:最简单的Django安装方法(Windows 7)
前提是你已经在机器上安装了python~并且你的机器能够上网~ 1.进入:https://bootstrap.pypa.io/get-pip.py,ctrl+s保存网页(其实是.py文件)到某一目录: ...