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

Aja*_*jay 7 c++ stl map std-pair

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

虽然我知道这是为了与其他STL容器兼容,但是展示了这一点value_type.但是find方法会key_type破坏这种兼容性断言.map既有key_typemapped_type,所以为什么不能map有:

iterator insert(const key_type&, const mapped_type&);
Run Code Online (Sandbox Code Playgroud)

是的,有insert迭代器的重载.但这两个论点insert可能会很好.

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

编辑:刚刚发现这insert唯一采用的方法value_type,即pair<X,Y>.许多其他的方法,如find,erase,at,count,equal_range,lower_bound,upper_boundoperator[]key_type.

Ker*_* SB 6

所有标准库容器定义一个value_type成员类型,它们的接口一般在这方面工作value_type:insert,push_back,push_front.新接口emplace添加了一种构造value_type对象的方法,如下所示:

value_type(std::forward<Args>(args)...)
Run Code Online (Sandbox Code Playgroud)

基本上,有规定的卫星数据关联容器(即图),这是知道的特殊结构的无特殊接口value_type(其被定义,不完全公知的,是pair<const key_type, mapped_type>),与外finderaseoperator[],这接受key_type争论.

它可能是对标准的疏忽,或者它从未被认为是一个问题,因为您总是可以使用make_pair,make_tuple或者forward_as_tuple,或者emplace,创建地图值类型.

(有一个问题,insert只有移动的映射类型已经浮出水面,并且是最近提案的主题.)