C++ 为什么 std::list 没有运算符 []?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1112724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't there an operator[] for a std::list?
提问by Gab Royer
Can anyone explain why isn't the operator[] implemented for a std::list? I've searched around a bit but haven't found an answer. It wouldn't be too hard to implement or am I missing something?
谁能解释为什么没有为 std::list 实现 operator[] ?我已经搜索了一下,但没有找到答案。实施起来不会太难,还是我错过了什么?
回答by Pavel Minaev
Retrieving an element by index is an O(n) operation for linked list, which is what std::list
is. So it was decided that providing operator[]
would be deceptive, since people would be tempted to actively use it, and then you'd see code like:
通过索引检索元素是链表的 O(n) 操作,这std::list
就是。所以决定提供operator[]
是欺骗性的,因为人们会很想积极地使用它,然后你会看到这样的代码:
std::list<int> xs;
for (int i = 0; i < xs.size(); ++i) {
int x = xs[i];
...
}
which is O(n^2) - very nasty. So ISO C++ standard specifically mentions that all STL sequences that support operator[]
should do it in amortized constant time (23.1.1[lib.sequence.reqmts]/12), which is achievable for vector
and deque
, but not list
.
这是 O(n^2) - 非常讨厌。所以 ISO C++ 标准特别提到,所有支持的 STL 序列都operator[]
应该在摊销常数时间 (23.1.1[lib.sequence.reqmts]/12) 内完成,这对于vector
和是可以实现的deque
,但不是list
。
For cases where you actually need that sort of thing, you can use std::advance
algorithm:
对于您确实需要那种东西的情况,您可以使用std::advance
算法:
int iter = xs.begin();
std::advance(iter, i);
int x = *iter;
回答by florin
It would not be too hard (for the implementer) but it would be too hard at runtime, since the performance will be terrible in most cases. Forcing the user to go through each link will make it more obvious what is going on in there than 'myList[102452]' would.
这不会太难(对于实现者),但在运行时会太难,因为在大多数情况下性能会很糟糕。强制用户通过每个链接将使那里发生的事情比“myList[102452]”更明显。
回答by Gab Royer
I think I found the answer in another SO post Extending std::list
我想我在另一个 SO post Extending std::list 中找到了答案
"your operator[] is O(N) time" - this is exactly why it is not included in the standard's std::list<>. – Michael Burr Dec 14 at 17:29
“您的运算符 [] 是 O(N) 时间”——这正是它不包含在标准的 std::list<> 中的原因。– 迈克尔·伯尔 12 月 14 日 17:29
Still, is that the only reason?
不过,这是唯一的原因吗?
EDIT : It seems though as people mentioned it is more a matter of consistency regarding performance then strictly performance.
编辑:似乎正如人们所提到的,它更多地是关于性能的一致性问题,而不是严格的性能问题。
回答by Dusan
Actually, there is absolutely no reason to not provide operator[] or at least method at(int), because of the two reasons:
实际上,绝对没有理由不提供 operator[] 或至少方法 at(int),因为有两个原因:
- It is double linked list, so you need to move at most size()/2 places your iterator to get your index, and costs to internally keep few more fixed iterators are very low. And at the end, the Qt library provides the operator[] and the at, and I do not see performance cost using it.
- forcing people something not to use is a very bad programming habit, because a list will much usable container, if you have a "random access" near the linked access, there is a variety examples when you need both access, depending at which runtime point.
- 它是双链表,因此您最多需要移动 size()/2 个迭代器来获取索引,并且内部保留更多固定迭代器的成本非常低。最后,Qt 库提供了 operator[] 和 at,我没有看到使用它的性能成本。
- 强迫人们不要使用某些东西是一个非常糟糕的编程习惯,因为列表将有很多可用的容器,如果您在链接访问附近有一个“随机访问”,那么当您需要两种访问时,有多种示例,具体取决于哪个运行时点.