如何更改地图容器的内部排序方案? [英] How do I change the map container's internal sorting scheme?

查看:51
本文介绍了如何更改地图容器的内部排序方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名C ++初学者,所以有些语言结构我无法理解,这使我无法理解map的API(供您参考,

I'm a beginner C++ programmer so there are language constructs that I don't understand which prevents me from understanding map's API (for your reference, here)

更重要的是,一些问题:

More to the point, some questions:

如何更改map的内部排序方案,以便在我们使用 map ::< string,...> 的情况下,键值按字母顺序排序?

How do I change the internal sorting scheme of map so that, given that we're working with map::<string, ...>, the key values are sorted alphabetically?

关于 map :: key_comp 的更具体的描述,是一次定义后遗忘的事情,一旦我们定义了相同类型的两个元素不相等(一个小于而不是另一个)",那么排序是在内部自动完成的-因此我们所需要做的就是插入键/值对?还是我们必须定义等式/排序,然后显式调用该函数以返回一个布尔值以实现有序插入?

More specifically about the map::key_comp, is it a define-and-forget thing where once we define what it means for two elements of the same type to be "unequal (one is less than the other)", then the sorting is done internally and automatically - so all we need to is insert key/value pairs? Or do we have to define equality/ordering and then call the function explicitly to return a boolean to implement ordered insertion?

推荐答案

下面是一个示例,说明如何为排序后的映射提供模板参数以使用非默认排序:

Here's an example of how you give the sorted map a template argument to use a non-default sort:

std::map<int, int, std::greater<int> > m;

来自 C ++ std :: map项,按键降序排列

还有一个更复杂的示例:如何声明自定义排序功能是否在std :: map声明上?

Also, for a more complex example: how to declare custom sort function on std::map declaration?

这篇关于如何更改地图容器的内部排序方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆