C++ 向量迭代器在 for 循环中不可解引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18636941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:04:49  来源:igfitidea点击:

Vector iterator not dereferencable in for loop

c++vector

提问by TPOT94

I'm using a loop to count how many times that a word was entered then print the word and how many times it was entered, which works but it never prints the last word, I have it sorted alphabetically. Before the last word is printed it errors out saying the iterator is not dereferencable. Here is my code for the loop:

我正在使用循环来计算输入单词的次数,然后打印该单词以及输入的次数,这可行,但它从不打印最后一个单词,我按字母顺序对其进行了排序。在打印最后一个单词之前,它会出错说迭代器不可解引用。这是我的循环代码:

for (vector<string>::iterator it = v.begin() ; it != v.end(); ++it)
    {
        if (*it == *(it+1))
        {
        count++;
        }
        else if (*it != *(it+1))
        {
                count++;
            cout << *it << " ---- " << count << endl;
            count=0;
        }
    }

回答by billz

Your code has undefined behavior - imagine itis pointing to the last element of v, then you are trying to dereference v.end()in *(it+1)

你的代码有不确定的行为-想象it指向的最后一个元素v,那么您要提领v.end()*(it+1)

if (*it != *(it+1)

STL iterator, end doesn't point to last element; end() returns an iterator that represents the end of the elements in the container. The end is the position behindthe last element. Such an iterator is also called a past-the-end iterator.

STL迭代器,end不指向最后一个元素;end() 返回一个迭代器,表示容器中元素的结尾。结尾是最后一个元素后面的位置。这样的迭代器也称为过去的迭代器

Thus, begin() and end() define a half-openrange that includes the first element but excludesthe last

因此,begin() 和 end() 定义了一个包含第一个元素但不包括最后一个元素的半开范围

 --------------------------------
 |  |   |   |   |   |   |   |   |
 --------------------------------
  /\                               /\      
begin()                            end() 

For what you are trying to achieve, have a look at std::adjacent_find

对于您想要实现的目标,请查看std::adjacent_find

auto it = std::adjacent_find(v.begin(), v.end());

if (it != v.end())
{
  count ++;
}
else
{
   cout << *it << " ---- " << count << endl;
}

回答by Jarod42

when it == v.end() - 1, you deference (it+1)so v.end(), and deference v.end()is undefined behaviour.

when it == v.end() - 1,你尊重(it+1)so v.end(),并且尊重v.end()是未定义的行为。

回答by Gorpik

When you are at the last word and try to execute:

当你说到最后一句话并尝试执行时:

if (*it == *(it+1))

if (*it == *(it+1))

it+1is pointing at v.end(), which is a valid iterator, but is not derefernceable. Hence the error.

it+1指向v.end(),这是一个有效的迭代器,但不可取消引用。因此错误。

回答by Neil Kirk

When it is one before the end iterator, you have a problem here: *(it+1)as this attempts to dereference the end iterator, which is invalid.

当它在结束迭代器之前是 1 时,这里有一个问题:*(it+1)因为这试图取消引用结束迭代器,这是无效的。

I'm not sure what you want your logic to do in this case, but you can check for this with if (it+1 != v.end())before doing your stuff.

我不确定在这种情况下你希望你的逻辑做什么,但你可以if (it+1 != v.end())在做你的事情之前检查这个。

回答by hate-engine

Because when itis near end, it+1is at end, and you trying to dereference it in ifoperator.

因为何时it接近结束,it+1在结束时,您试图在if运算符中取消引用它。