English 中文(简体)
为什么会推回一个 st子:将变换成一个以大概为开端的反导器?
原标题:Why does a push_back on an std::list change a reverse iterator initialized with rbegin?

根据一些STL文件,我发现、插入或删除了下列内容:清单不会使清单失效。 也就是说,允许在一份清单(从begin()到end(<>>/code>>)上登记,然后添加使用推介的成分。

例如,在以下法典中,我先提出一个包含要素(a、b和c)的清单,然后将清单交给它处理,并对内容进行推论。 其结果应是ba巴布,这正是我得到的:

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(<>/code>)并使用推回机时,我将期望类似行为,即诱发诱发的结果。 然而,我取得了不同的结果:

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。 添加了一条。

它看起来像首批推回一样,也改变了以暴发为先发制人的价值。 在推回之后,它不再指名单上第3个要素(以前是最后一个要素),而是指第4个要素(现在是最后一个要素)。

我通过“2010年视觉演播室”和“海湾合作委员会”测试了这一结果,两者都回报了同样的结果。

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

最佳回答

该标准指出,插文中的索引和参考资料仍然有效。 它对逆向渗透者说什么。

。 在<代码>push_back(>之后,这一数值显然与以前并无相同。 我不认为标准是应该的。 明显替代物包括清单的前几个要素,或者如果是固定价值(例如,单列单)则在最后。


技术细节: 由<代码>rend()退还的价值在<代码>begin(之前不能注明,因为该数值无效。 因此,决定<代码>rend()应包含begin(,所有其他反向索引应进一步改变一个立场。 <代码>operator*对这一点进行补偿,并可在任何方面查阅正确的内容。

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

问题回答

我想理解这一点,最好首先重新制定<<>><>>>><>>>>>。 代表:

typedef std::list<std::string> container;

container testList;
testList.push_back("a");
testList.push_back("b");
testList.push_back("c");

container::reverse_iterator itList = testList.rbegin(); 
while (itList != testList.rend()) {
    testList.push_back(*itList);
     ++itList;
}

与此同时,我们必须理解“<条码>反向_iterator的总体运作情况。 具体来说,reverse_iterator 在<>>之后插入以下内容: 各位在推荐时就得了。 。 集装箱的关闭——但对于像阵列这样的货物,在集装箱开始之前就没有确定的途径。 相反,C++的操作者从尾声开始,到一开始就取得了进展,但当你不提这一点时,在实际点点点上,你才获得“之前”。

这意味着你的法典实际上就是这样:

“enter

之后,你得到的正是你所期望的,又回到了B,然后是A,因此,你最终与ABCCCBA站站在一起。

双方均使用电梯。 Try:

std::list<std::string>::iterator i = testList.end(); 

页: 1





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?