有没有设计的原因,为什么std :: set不具有前面和后面的成员函数? [英] Is there a design reason why std::set doesnt have front and back member functions?

查看:357
本文介绍了有没有设计的原因,为什么std :: set不具有前面和后面的成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我知道我可以使用 * s.begin(),但是相同的参数可以用于 / code>,其中包含 / 返回

  2. 我使用set / map的有序属性来获得最小元素/键 - ofc的事实这不是它的原因,只是一个例子:)

  1. I know I can use *s.begin(), but same argument can be used for vector, which has front/back
  2. often I use the ordered property of set/map to get "smallest" element/key - ofc the fact that I do it is not the reason to have it, just a example :)

这里我谈的是为什么 / back 将是不好的设计,所以请跳过显而易见的原因,如委员会忘记了...

Here I'm talking about design reasons why front/back would be bad design, so please skip obvious reasons like committee forgot about it...

推荐答案

我想象前面和后面是为序列容器(即元素的顺序由插入顺序决定的)保留的,

I imagine that the words "front" and "back" are reserved for sequence containers (i.e. those where the order of elements is determined by the order of insertion), and the words are meant to suggest a physical position in that sequence.

由于设置不是序列容器(而是一个关联容器),这是不合适的。特别要注意,front的含义可以通过稍后插入一个不相关的元素来改变。

Since set is not a sequence container (but instead an associative container), this isn't appropriate. In particular, note that the meaning of "front" can change by later insertions of an unrelated element.

这篇关于有没有设计的原因,为什么std :: set不具有前面和后面的成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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