地图比较构造函数参数 [英] map Comparison Constructor Parameter

查看:122
本文介绍了地图比较构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有理由为什么我不能将比较函子传递给地图作为构造函数参数:

Is there a reason why I cannot pass a comparison functor to a map as a constructor argument:

map<int, string, greater<int>> foo;
//map<int, string> foo(greater<int>()); Doesn't work

或者为什么我不能传递lambda而不提供我自己的比较类型:

Or why I cannot pass a lambda without providing my own comparison type:

map<int, string, function<bool(const int&, const int&)>> bar([](const int& lhs, const int& rhs){ return lhs > rhs; });
//map<int, string> bar([](const int& lhs, const int& rhs){ return lhs > rhs; }); Doesn't work

我想只能声明 map< ; int,string> 并用比较器构造它。为什么我不能?

I'd like to just be able to declare map<int, string> and construct it with a comparator. Why can't I?

[实例]

[Live Example]

推荐答案

这个问题源于一个误解。要澄清:

This question stems from a misconception. To clear that up:

Functors是对象功能

Functors are objects not functions

尝试为一个对象分配一个函数指针或一个lambda没有任何意义。所以这是无法做到的: map< int,string> bar([](const int& lhs,const int& rhs){return lhs> rhs;}); 定义 code>它使用函数指针或lambda是使用问题的模板参数: map< int,string,function< bool(const int& const int&)>>>

Trying to assign a function pointer or a lambda to an object doesn't make any sense. So this cannot be done: map<int, string> bar([](const int& lhs, const int& rhs){ return lhs > rhs; }); The way to define a map which takes a function pointer or lambda is to use the template arguments from the question: map<int, string, function<bool(const int&, const int&)>>

这个问题中两个不合理的选项之间的一半是另一个误解: map< int, string,[](const int& lhs,const int& rhs){return lhs> RHS; }> 不起作用,因为比较模板参数是成员类型 >,初始化值。 所以使用函数指针或lambda比较器的映射必须始终将该值传递给映射构造函数: map< int,string,function< bool(const int& const int&)>> bar([](const int& lhs,const int& rhs){return lhs> rhs;}) 否则 function< bool(const int&将用于地图中的比较。

Halfway between the two ill-conceived options in the question is another misconception: map<int, string, [](const int& lhs, const int& rhs){ return lhs > rhs; }> Doesn't work because the comparator template argument is the type of a member of map, not the intialization value. So a map using a function pointer or lambda comparator must always have that value passed to the map constructor: map<int, string, function<bool(const int&, const int&)>> bar([](const int& lhs, const int& rhs){ return lhs > rhs; }) Otherwise function<bool(const int&, const int&)>() would be used for comparisons in the map.

现在这可能已经很清楚了,但是由于functor是对象,你不能通过一个不相关的对象是一个完全不同类型的对象的构造值。调用 map< int,string> foo(greater< int>()) 就像调用 less< int> foo = greater< int> 对于映射,其比较模板参数是一个函子的唯一可接受的兼容构造函数参数是可以转换为模板参数中函数类型对象的东西: map< int,string,greater< int>> foo(更大< int> {}) 这显然是不必要的,因为如果没有提供参数,而更大< int> 默认构造了相同的成员初始化映射将导致,所以 map< int,string,greater< int>> 是足够的。

This is probably already clear by now, but since functors are objects you cannot pass an unrelated object is the construction value of a completely different type of object. Calling map<int, string> foo(greater<int>()) is like calling less<int> foo = greater<int>. The only acceptable compatator constructor argument to a map whose comparator template argument is a functor is something that can be converted into an object of the functor type in the template argument: map<int, string, greater<int>> foo(greater<int>{}) This is obviously unnecessary, because if no argument was provided and the greater<int> was default constructed the same member initialization of the map would result, so map<int, string, greater<int>> is sufficent.

这篇关于地图比较构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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