排序地图不区分大小写

时间:2016-06-14 09:01:15

标签: c++ dictionary

Map隐式排序其中的元素,但有没有办法强制map对元素进行排序不敏感?

使用这段代码

map <string , string> m;
m["Axion"] = "second";
m["Beta"] = "third";
m["alpha"] = "first";

for( auto &x : m ){
   cout << x.second << endl;
}

它会输出:

second
third
first

duo到大写字母,其ascii代码小于小写字符。

有没有办法如何强制地图对它进行排序,以便输出

"first"
"second"
"third"

1 个答案:

答案 0 :(得分:0)

std::map<>的第三个模板参数是一个比较函数,只是提供它来做一个不区分大小写的比较......

template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >
> class map;