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

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

问题描述

STL参考资料似乎在以下概念上有所区别:

The STL reference seems to make a conceptual difference between :


  • '序列容器'(array vector deque forward_list list)一方面

  • '关联容器'(set multiset map multimap unordered_set unordered_multiset unordered_map unordered_multimap)另一方面。

另外,似乎我们有:


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

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

  • 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.

我的理解是, front()方法可以很容易地根据 begin()方法通过取消引用其返回值。

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

因此,我的问题是:为什么不为定义 begin()方法的所有对象定义 front() strong>(这应该是每个容器真的)

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).

推荐答案

你真的必须要求标准委员会(comp.lang.c ++。std),但我猜测是的,这只是没有多大意义。此外,还没有什么明确的意思。你想要根,预订第一,订单先,先插入吗?顺序很清楚:前面是一边,背对另一边。地图是树。

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天全站免登陆