繁体   English   中英

为什么需要将对插入地图?

[英]Why pair is required to insert into map?

虽然我不喜欢它,但发现声明一pair<X,Y>对象或调用make_pair ,以便调用map::insertmake_pair 为什么insert不使用两个参数来分别指定Key和Value。

虽然我理解它是为了与其他STL容器兼容,但它展示了value_type 但是find方法采用key_type来破坏这种兼容性断言。 map既有key_type又有mapped_type ,所以为什么cant map有:

iterator insert(const key_type&, const mapped_type&);

是的, insert迭代器有重载。 但是这个两个参数insert可能混合得很好。

我看到的一个优点是:减少了调用堆栈的使用。

编辑 :刚刚发现insert唯一采用value_type方法,即pair<X,Y> 许多其他方法,如finderaseatcountequal_rangelower_boundupper_boundoperator[]采用key_type

所有标准库容器都定义了一个value_type成员类型,它们的接口通常按照value_typeinsertpush_backpush_front 新的接口emplace添加了一种构造value_type对象的方法,就像通过:

value_type(std::forward<Args>(args)...)

基本上,没有为卫星数据关联容器(即地图)提供特殊的接口,这些容器知道value_type的特殊结构(已定义,不是完全众所周知的,是pair<const key_type, mapped_type> ),除了finderase以及key_type参数的operator[] key_type

这也许是标准的监督,或许它从未被认为是一个问题,因为你可以随时使用make_pairmake_tupleforward_as_tuple ,或emplace ,创建映射值的类型。

insert和仅移动映射类型存在一个问题,它已浮出水面并且是最近提案的主题。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM