【发布时间】:2019-05-26 23:52:44
【问题描述】:
以下代码在发布模式下有效,并且仅在调试模式下在 g_ItemList.push_back() 处引发错误,我浏览了一些 SO 帖子和论坛。有人提到"You can't iterate through a vector while pushing back, as push_back may invalidate iterators"。但对我来说还不清楚。有人可以解释它背后的逻辑以及如何解决这个问题吗?我正在使用 Visual Studio 2010 (v100) 编译程序
std::vector<class PPart> VECT_ITEMS;
VECT_ITEMS g_ItemList;
g_ItemList.clear();
for (i = 0; i < n; i++)
g_ItemList.push_back (temp[i]); //where PPart *temp;
【问题讨论】:
-
请提供minimal reproducible example。
temp和n是如何初始化的?