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

      <tfoot id='NYOaa'></tfoot>
      1. <small id='NYOaa'></small><noframes id='NYOaa'>

      2. <legend id='NYOaa'><style id='NYOaa'><dir id='NYOaa'><q id='NYOaa'></q></dir></style></legend>

      3. 为什么 std::list 上的 push_back 会更改用 rbegin 初始化的反向迭代器?

        时间:2024-05-11
          <tbody id='sPQEt'></tbody>
          <bdo id='sPQEt'></bdo><ul id='sPQEt'></ul>

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

              • <legend id='sPQEt'><style id='sPQEt'><dir id='sPQEt'><q id='sPQEt'></q></dir></style></legend>
              • <small id='sPQEt'></small><noframes id='sPQEt'>

                  本文介绍了为什么 std::list 上的 push_back 会更改用 rbegin 初始化的反向迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  根据我发现的一些 STL 文档,在 std::list 中插入或删除元素不会使迭代器失效.这意味着允许循环遍历一个列表(从 begin()end()),然后使用 push_front 添加元素.

                  According to some STL documentation I found, inserting or deleting elements in an std::list does not invalidate iterators. This means that it is allowed to loop over a list (from begin() to end()), and then add elements using push_front.

                  例如,在下面的代码中,我用元素 a、b 和 c 初始化一个列表,然后遍历它并执行元素的 push_front.结果应该是cbaabc,这正是我得到的:

                  E.g., in the following code, I initialize a list with elements a, b and c, then loop over it and perform a push_front of the elements. The result should be cbaabc, which is exactly what I get:

                  std::list<std::string> testList;
                  testList.push_back("a");
                  testList.push_back("b");
                  testList.push_back("c");
                  
                  for (std::list<std::string>::iterator itList = testList.begin(); itList != testList.end(); ++itList)
                     testList.push_front(*itList);
                  
                  for (std::list<std::string>::const_iterator itList = testList.begin(); itList != testList.end(); ++itList)
                     std::cout << *itList << std::endl;
                  

                  当我使用反向迭代器(从 rbegin()rend() 的循环)并使用 push_back 时,我希望有类似的行为,即 abccba.但是,我得到了不同的结果:

                  When I use reverse iterators (loop from rbegin() to rend()) and use push_back, I would expect similar behavior, i.e. a result of abccba. However, I get a different result:

                  std::list<std::string> testList;
                  testList.push_back("a");
                  testList.push_back("b");
                  testList.push_back("c");
                  
                  for (std::list<std::string>::reverse_iterator itList = testList.rbegin(); itList != testList.rend(); ++itList)
                     testList.push_back(*itList);
                  
                  for (std::list<std::string>::const_iterator itList = testList.begin(); itList != testList.end(); ++itList)
                     std::cout << *itList << std::endl;
                  

                  结果不是abccba,而是abcccba.没错,添加了一个额外的 c.

                  The result is not abccba, but abcccba. That's right there is one additional c added.

                  看起来第一个 push_back 也改变了用 rbegin() 初始化的迭代器的值.在 push_back 之后,它不再指向列表中的第三个元素(以前是最后一个),而是指向第四个元素(现在是最后一个).

                  It looks like the first push_back also changes the value of the iterator that was initialized with rbegin(). After the push_back it does not point anymore to the 3rd element in the list (which was previously the last one), but to the 4th element (which is now the last one).

                  我使用 Visual Studio 2010 和 GCC 对此进行了测试,两者都返回相同的结果.

                  I tested this with both Visual Studio 2010 and with GCC and both return the same result.

                  这是一个错误吗?或者我不知道的反向迭代器的一些奇怪行为?

                  Is this an error? Or some strange behavior of reverse iterators that I'm not aware of?

                  推荐答案

                  标准规定迭代器和引用在插入期间保持有效.它没有说明反向迭代器.:-)

                  The standard says that iterators and references remain valid during an insert. It doesn't say anything about reverse iterators. :-)

                  rbegin() 返回的 reverse_iterator 在内部保存了 end() 的值.在 push_back() 之后,这个值显然与之前不同.我认为标准没有说明它应该是什么.明显的替代方案包括列表的前一个最后一个元素,或者如果它是一个固定值(如哨兵节点),则它留在最后.

                  The reverse_iterator returned by rbegin() internally holds the value of end(). After a push_back() this value will obviously not be the same as it was before. I don't think the standard says what it should be. Obvious alternatives include the previous last element of the list, or that it stays at the end if that is a fixed value (like a sentinel node).

                  技术细节:rend() 返回的值不能指向 begin() 之前,因为那是无效的.因此决定 rend() 应该包含 begin() 的值,并且所有其他反向迭代器进一步移动一个位置.operator* 对此进行了补偿,并且无论如何都会访问正确的元素.

                  Technical details: The value returned by rend() cannot point before begin(), because that is not valid. So it was decided that rend() should contain the value of begin() and all other reverse iterators be shifted one position further. The operator* compensates for this and accesses the correct element anyway.

                  24.5.1 反向迭代器的第一段说:

                  First paragraph of 24.5.1 Reverse iterators says:

                  类模板 reverse_iterator 是一个迭代器适配器,从定义的序列的末尾开始迭代通过其底层迭代器到该序列的开头.反向之间的基本关系迭代器及其对应的迭代器 i 由标识建立:
                  &*(reverse_iterator(i)) == &*(i - 1).

                  Class template reverse_iterator is an iterator adaptor that iterates from the end of the sequence defined by its underlying iterator to the beginning of that sequence. The fundamental relation between a reverse iterator and its corresponding iterator i is established by the identity:
                  &*(reverse_iterator(i)) == &*(i - 1).

                  这篇关于为什么 std::list 上的 push_back 会更改用 rbegin 初始化的反向迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++/STL 是否支持按属性对对象进行排序? 下一篇:只读操作的 std::map 线程安全

                  相关文章

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

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

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

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