为什么在std :: map(和STL的其他关联容器)上没有front()方法? [英] Why no front() method on std::map (and other associative containers from the STL)?

查看:128
本文介绍了为什么在std :: map(和STL的其他关联容器)上没有front()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

STL参考似乎在概念上有所不同:




  • 一方面

  • '关联容器'(set multiset map multimap unordered_set unordered_multiset unordered_map unordered_multimap)



此外,看起来我们有:




  • 所有实现 begin()方法的容器返回指向容器中第一个元素的迭代器。

  • 只有具有 front()方法的序列容器返回对容器中第一个元素的引用。

$ b我的理解是 front()方法可以很容易地定义 begin()为什么不是前面(> ) c>方法 c> > b
$ b

(我猜想从语义的角度来看,从一个map获取第一个元素和从一个vector中获得第一个元素没什么区别,但我是不知道是否有更有效的解释。)

解决方案

你真的要问一个标准委员会.c ++。std)但我的猜测是,是的,它只是没有多大的意义。此外,没有什么清楚,这是什么意思。你想要根,预订第一,后序先,首先你插入...?有序列很清楚:前面是一面,后面另一面。地图是树木。


The STL reference seems to make a conceptual difference between :

  • 'Sequence containers' (array vector deque forward_list list) on one hand
  • 'Associative containers' (set multiset map multimap unordered_set unordered_multiset unordered_map unordered_multimap) on the other hand.

Also, it seems like we have :

  • all containers implementing a begin() method returning an iterator pointing to the first element in the container.
  • only the sequence containers having a front() method returning a reference to the first element in the container.

My understanding is that the front() method could easily be defined in terms of the begin() method by just dereferencing its return value.

Thus, my question is : why isn't the front() method defined for all objects defining the begin() method ? (which should be every container really)

(I guess that from a semantic point of view, it doesn't make as much sense to get the first element from a map as it does for the first element from a vector but I was wondering if there was a more valid explanation).

解决方案

You really have to ask the standards committee on that one (comp.lang.c++.std) but my guess is that yeah, it just doesn't make as much sense. Further there's not as much clarity as to what it would mean. Do you want the root, the pre-order first, post-order first, first you inserted...? With sequences it's quite clear: front is one side, back the other. Maps are trees.

这篇关于为什么在std :: map(和STL的其他关联容器)上没有front()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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