std::map 设计:为什么 Map 接受比较器作为模板参数

发布于 2024-07-17 01:27:07 字数 597 浏览 3 评论 0原文

STL 中的 Map 类型有下一个类型:

std::map< Key, Data, Compare, Alloc >

作为模板参数之一,我们可以传递 Compare 谓词,为什么 Map 接受这个谓词作为模板参数而不是构造函数中的对象?

它可以具有更灵活的接口,例如 boost::functionboost::functionboost::functionboost::functionboost::functionboost::functionbool、const T&、const T& > 在构造函数中。
当然,我知道在设计STL时boost并不存在,但设计者可以在boost::function上创建类似的东西。

我相信这其中有一些深层次的原因。

已编辑
抱歉,这是个虚拟问题,地图也有同样的可能性:)
在你的回答之后我的问题没有任何意义。

Map type from STL have next type:

std::map< Key, Data, Compare, Alloc >

As one of template parameters we could pass Compare predicate, why map accept this predicate as template parameter and not as object in constructor?

It could has more flexible interface with something like boost::function< bool, const T&, const T& > in constructor.
Ofcourse I'm understend that when STL was designed boost does not exists, but designers could be create something similar on boost::function.

I belive it has some deep reasons.

EDITED
Sorry for dummy question, map have same posibility :)
My question doesn't have sense after your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧竹 2024-07-24 01:27:07

模板参数是谓词的类型,而不是值。 值可以作为构造函数的参数提供。 您可以指定与该类型匹配的任何值。 如给定的,默认类型是 std::less,它几乎只有一个值,但您应该能够为 Compare 指定自己的类型参数,包括 boost::function,然后使用各种值来控制地图对象的行为。

The template argument is the type of the predicate, not the value. The value can be provided as an argument to the constructor. You can specify any value that matches the type. As given, the default type is std::less<Key>, which pretty much only has one value, but you should be able to specify your own type for the Compare argument, including boost::function, and then use various values to control the behavior of your map objects.

何止钟意 2024-07-24 01:27:07

Map 确实有这样的构造函数。 来自 C++ 标准第 23.3.1 节:

explicit map(const Compare& comp = Compare(),
const Allocator& = Allocator());

Map DOES have such a constructor. From section 23.3.1 of the C++ Standard:

explicit map(const Compare& comp = Compare(),
const Allocator& = Allocator());
七七 2024-07-24 01:27:07

由于 boost::function 是多态的,因此无法内联。 STL的设计旨在最大限度地发挥编译器内联代码的潜力,这在扩展模板上很容易实现; 如果您需要它是多态的,您也可以轻松地决定使用 boost::function 来提供与 std::map 的比较。

Because boost::function is polymorphic, it cannot be inlined. The design of the STL is aimed at maximum potential for the compiler to do inlining of code, which is easy on expanded templates; also you could easily make the decision to use boost::function to supply the comparison with std::map if you needed it to be polymorphic.

缺⑴份安定 2024-07-24 01:27:07

使用比较对象会产生运行时成本 - 需要存储该对象,并且必须通过指针进行比较。 通过使用类,比较可以简化为单个表达式,例如使用 int 键时。 标准库的目标是不低于优秀 C++ 程序员自己生成的库的效率。

Using a compare object creates a run-time cost - the object needs to be stored, and the comparison must occur through a pointer. By using a class, the comparison can simplify down into a single expression, for example when using int keys. The goal of the standard library was to be no less efficient than what a good C++ programmer would generate on their own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文