English 中文(简体)
BOOST_FOREACH
原标题:Modifying contents of vector in BOOST_FOREACH

这个问题涉及到巴塞尔联络处如何发挥作用。 表格检查了休息时间。

cout << "Testing BOOST_FOREACH" << endl;
vector<int> numbers; numbers.reserve(8);
numbers.push_back(1); numbers.push_back(2); numbers.push_back(3);
cout << "capacity = " << numbers.capacity() << endl;
BOOST_FOREACH(int elem, numbers)
{
    cout << elem << endl;
    if (elem == 2) numbers.push_back(4); 
}
cout << "capacity = " << numbers.capacity() << endl;

产出

Testing BOOST_FOREACH
capacity = 8
1
2
3
capacity = 8

但是,第4号究竟是怎样的? 如果我把新插入的编号改为名单,将予以更新。 病媒推回行动将使任何点人都失去功能,但需要重新定位,但这种情况并未发生。 因此,我猜测的问题是,为何只用病媒对终端(在循环之前)器进行评价,而在使用清单时进行更有活力的评价?

最佳回答

Under the covers, BOOST_FOREACH uses iterators to traverse the element sequence. Before the loop is executed, the end iterator is cached in a local variable. This is called hoisting, and it is an important optimization. It assumes, however, that the end iterator of the sequence is stable. It usually is, but if we modify the sequence by adding or removing elements while we are iterating over it, we may end up hoisting ourselves on our own petard.

如果您不希望达到目的,则改变病媒的利用,而不是保留。

http://www.cplus.com/vis/stl/vector/resize/

请注意,你不想推回,而是使用经营者。 但是,必须谨慎地摆脱约束。

问题回答

在评论中提出了问题,即Microsoft debug的操作时间为何在传导过程中引起对病媒的断言,但并未超出清单。 原因是insert在<代码>>>>>>>>上的定义不同。 (注:push_back is only an insert at the end of the series.

《C++标准》(ISO/IEC 14882:2003 23.2.4.3,vector modifiers):

[插入],如果不重新定位,在插入点之前的所有探测器和参考资料仍然有效。

(23.2.2.3,list modifiers):

[插入]不影响变体和参考资料的有效性。

因此,如果你使用<代码>push_back (并且肯定不会造成重新定位),它用任何集装箱继续使用你的载体在顺序的其余部分中加以回收。

但是,在病媒方面,它使用<条码>的>明确行为。 您在<代码>push_back之前获得的电传器。

www.un.org/Depts/DGACM/index_spanish.htm 这是对问题的四舍五入回答,是对问题讨论的直接回答。

推进器=数字后,推进器将终止。

固然如此,呼吁追捕会/将使你现有的任何遗体失效。





相关问题
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?

热门标签