<i id='BVxs9'><tr id='BVxs9'><dt id='BVxs9'><q id='BVxs9'><span id='BVxs9'><b id='BVxs9'><form id='BVxs9'><ins id='BVxs9'></ins><ul id='BVxs9'></ul><sub id='BVxs9'></sub></form><legend id='BVxs9'></legend><bdo id='BVxs9'><pre id='BVxs9'><center id='BVxs9'></center></pre></bdo></b><th id='BVxs9'></th></span></q></dt></tr></i><div id='BVxs9'><tfoot id='BVxs9'></tfoot><dl id='BVxs9'><fieldset id='BVxs9'></fieldset></dl></div>
    • <bdo id='BVxs9'></bdo><ul id='BVxs9'></ul>

    <legend id='BVxs9'><style id='BVxs9'><dir id='BVxs9'><q id='BVxs9'></q></dir></style></legend>
    <tfoot id='BVxs9'></tfoot>

    1. <small id='BVxs9'></small><noframes id='BVxs9'>

        为什么 std::list 没有运算符 []?

        时间:2024-05-11

      1. <i id='j7ms3'><tr id='j7ms3'><dt id='j7ms3'><q id='j7ms3'><span id='j7ms3'><b id='j7ms3'><form id='j7ms3'><ins id='j7ms3'></ins><ul id='j7ms3'></ul><sub id='j7ms3'></sub></form><legend id='j7ms3'></legend><bdo id='j7ms3'><pre id='j7ms3'><center id='j7ms3'></center></pre></bdo></b><th id='j7ms3'></th></span></q></dt></tr></i><div id='j7ms3'><tfoot id='j7ms3'></tfoot><dl id='j7ms3'><fieldset id='j7ms3'></fieldset></dl></div>

            <legend id='j7ms3'><style id='j7ms3'><dir id='j7ms3'><q id='j7ms3'></q></dir></style></legend>

            1. <small id='j7ms3'></small><noframes id='j7ms3'>

                <bdo id='j7ms3'></bdo><ul id='j7ms3'></ul>
              • <tfoot id='j7ms3'></tfoot>

                    <tbody id='j7ms3'></tbody>

                  本文介绍了为什么 std::list 没有运算符 []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能解释为什么没有为 std::list 实现 operator[] ?我已经搜索了一点,但没有找到答案.实施起来不会太难,还是我遗漏了什么?

                  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?

                  推荐答案

                  通过索引检索元素是链表的 O(n) 操作,这就是 std::list 的含义.因此决定提供 operator[] 将具有欺骗性,因为人们会很想积极地使用它,然后你会看到如下代码:

                  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:

                   std::list<int> xs;
                   for (int i = 0; i < xs.size(); ++i) {
                       int x = xs[i];
                       ...
                   }
                  

                  这是 O(n^2) - 非常讨厌.所以ISO C++标准特别提到所有支持operator[]的STL序列都应该在分摊常数时间(23.1.1[lib.sequence.reqmts]/12)内完成,这对于vector 和 deque,但不是 list.

                  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.

                  如果你真的需要那种东西,你可以使用 std::advance 算法:

                  For cases where you actually need that sort of thing, you can use std::advance algorithm:

                  int iter = xs.begin();
                  std::advance(iter, i);
                  int x = *iter;
                  

                  这篇关于为什么 std::list 没有运算符 []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使 map::find 操作不区分大小写? 下一篇:与 win32 CRITICAL_SECTION 相比的 std::mutex 性能

                  相关文章

                  <small id='Gn6G8'></small><noframes id='Gn6G8'>

                    • <bdo id='Gn6G8'></bdo><ul id='Gn6G8'></ul>

                  1. <tfoot id='Gn6G8'></tfoot>

                    1. <i id='Gn6G8'><tr id='Gn6G8'><dt id='Gn6G8'><q id='Gn6G8'><span id='Gn6G8'><b id='Gn6G8'><form id='Gn6G8'><ins id='Gn6G8'></ins><ul id='Gn6G8'></ul><sub id='Gn6G8'></sub></form><legend id='Gn6G8'></legend><bdo id='Gn6G8'><pre id='Gn6G8'><center id='Gn6G8'></center></pre></bdo></b><th id='Gn6G8'></th></span></q></dt></tr></i><div id='Gn6G8'><tfoot id='Gn6G8'></tfoot><dl id='Gn6G8'><fieldset id='Gn6G8'></fieldset></dl></div>

                      <legend id='Gn6G8'><style id='Gn6G8'><dir id='Gn6G8'><q id='Gn6G8'></q></dir></style></legend>