“向量”中的这段代码是什么意思? 意思是? (C++)

发布于 2024-07-19 05:24:41 字数 1322 浏览 6 评论 0原文

我创建了一个程序,它使用了 vector.h #include 和迭代器等...但是当我运行该程序时,在某些情况下(我仍在试图弄清楚那些会是什么)我得到一个断言将我引用到 vector.h 的第 98 行时出错。 我转到了 vector.h 的第 98 行,得到了这个:

 #if _HAS_ITERATOR_DEBUGGING
        if (this->_Mycont == 0
            || _Myptr < ((_Myvec *)this->_Mycont)->_Myfirst
            || ((_Myvec *)this->_Mycont)->_Mylast <= _Myptr)
            {
            _DEBUG_ERROR("vector iterator not dereferencable");
            _SCL_SECURE_OUT_OF_RANGE;
            }

有人可以告诉我这意味着什么以及我的程序中是什么导致了这个断言吗?

注意:根据记录,第 98 行是以“_DEBUG_ERROR("vect...”开头的行。

注意:这是我的程序中的代码,我认为触发了错误,但我并不完全确定。

代码:

for(aI = antiviral_data.begin(); aI < antiviral_data.end();)
    {
        for(vI = viral_data.begin(); vI < viral_data.end();)
        {
            if((*aI)->x == (*vI)->x && (*aI)->y == (*vI)->y)
            {
                vI = viral_data.erase(vI);
                aI = antiviral_data.erase(aI);
            }
            else
            {
                vI++;
            }
        }
        if((*aI)->x >= maxx || (*aI)->x < 0 || (*aI)->y >= maxy || (*aI)->y < 0)
        {
            aI = antiviral_data.erase(aI);
        }
        else
        {
            aI++;
        }
    }

I created a program, and it uses the vector.h #include, and iterators, etc... But when I run the program, under certain circumstances (I'm still trying to figure out what those would be) I get an assertion error refering me to line 98 of vector.h. I went to line 98 of vector.h and got this:

 #if _HAS_ITERATOR_DEBUGGING
        if (this->_Mycont == 0
            || _Myptr < ((_Myvec *)this->_Mycont)->_Myfirst
            || ((_Myvec *)this->_Mycont)->_Mylast <= _Myptr)
            {
            _DEBUG_ERROR("vector iterator not dereferencable");
            _SCL_SECURE_OUT_OF_RANGE;
            }

Can somebody please tell me what this means and what in my program is causing this assertion?

NB: Line 98, for the record, is the one that begins "_DEBUG_ERROR("vect..."

NB: This is the code in my program that I BELIEVE triggered the error, I'm not entirely sure, though.

CODE:

for(aI = antiviral_data.begin(); aI < antiviral_data.end();)
    {
        for(vI = viral_data.begin(); vI < viral_data.end();)
        {
            if((*aI)->x == (*vI)->x && (*aI)->y == (*vI)->y)
            {
                vI = viral_data.erase(vI);
                aI = antiviral_data.erase(aI);
            }
            else
            {
                vI++;
            }
        }
        if((*aI)->x >= maxx || (*aI)->x < 0 || (*aI)->y >= maxy || (*aI)->y < 0)
        {
            aI = antiviral_data.erase(aI);
        }
        else
        {
            aI++;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小嗷兮 2024-07-26 05:24:41

运行时检测到您正在取消引用 begin() 之前或 end() 之后的迭代器。

想象一下,如果您删除第 7 行 antiviral_data 向量中的最后一项:

aI = antiviral_data.erase(aI);

aI 被设置为 antiviral_data.end(),并且当您取消引用时它在第 14 行:

if((*aI)->x >= maxx ...

以及第 5 行:

if((*aI)->x == (*vI)->x

您正在取消引用越界迭代器。

修复方法是在擦除调用后检查 aI != antiviral_data.end() ,以确保在继续使用向量之前没有到达向量的末尾。

The runtime is detecting that you are dereferencing an iterator that is before begin() or after end().

Imagine if you delete the last item in the antiviral_data vector in line 7:

aI = antiviral_data.erase(aI);

aI gets set to antiviral_data.end(), and when you dereference it in line 14:

if((*aI)->x >= maxx ...

and also in line 5:

if((*aI)->x == (*vI)->x

You are dereferencing an out of bounds iterator.

The fix is to check that aI != antiviral_data.end() after the erase call to make sure you haven't hit the end of the vector before you continue on using it.

欢烬 2024-07-26 05:24:41

您确实想查看像 remove_if 这样的 STL 算法,而不是手动执行这些操作。

You really want to look at STL algorithms like remove_if instead of doing this stuff manually.

胡渣熟男 2024-07-26 05:24:41

一个小的一般性评论:在检查 end() 迭代器时,不要使用“<”,而只能使用“!=”。 因此,代码的第一行应该如下所示:

for(aI = antiviral_data.begin(); aI != antiviral_data.end();)
{
  for(vI = viral_data.begin(); vI != viral_data.end();)
  {
    ...

然而,正如 Josh 已经指出的那样,您的具体错误位于第 7 行。

A small general comment: When checking an iterator for end(), do not use "<" but only "!=". So, the first lines of your code should look like:

for(aI = antiviral_data.begin(); aI != antiviral_data.end();)
{
  for(vI = viral_data.begin(); vI != viral_data.end();)
  {
    ...

However, as Josh already pointed, your specific bug is in line 7.

夜无邪 2024-07-26 05:24:41

除了已接受的答案之外,还要详细说明 slavy13 的答案 -
编辑 - 正如乔什所提到的,与这个问题直接相关 - 我将其留在这里供参考)。

代码(但不是此代码)有时假设您可以从向量中删除元素,并继续迭代。 这是一个错误的假设 - 一旦从向量中删除一个元素,被删除元素后面的所有其他迭代器都将失效 - 您不能再假设它们是正确的,如果继续使用它们,可能会发生“坏事”。

这样做的原因是因为向量实际上以数组形式存储信息。 当删除一个元素时,所有后续元素都会向下复制一个单元格。 迭代器不会相应更新。

强烈建议在尝试执行此类操作时查阅 STL 文档,因为此类代码完全有可能意外地在 STL 的某个实现上运行,但在其他实现上失败。

In addition to the accepted answer, and to elaborate on slavy13's answer -
(EDIT - and as mentioned by Josh, not directly relevant to this question - I'm leaving it here for reference).

Code (but not this code) sometimes assumes that you can remove elements from a vector, and keep iterating. This is a false assumption - once you remove an element from a vector, all other iterators following the removed element are invalidated - you can no longer assume they are correct, and "bad things" can happen if you keep using them.

The reason for this is because a vector actually stores information in an array form. When an element is removed, all following elements are copied one cell down. The iterators aren't updated accordingly.

It is strongly recommended to consult STL documentation whenever trying to do such things, because it is entirely possible such code would work on a certain implementation of STL accidentally, but fail on others.

贪了杯 2024-07-26 05:24:41

删除向量中的元素会使所有迭代器无效。

erasing an element in a vector invalidates all iterators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文