简体   繁体   English

向量迭代器不可取消(尝试手动反向向量)

[英]Vector Iterator not dereferencable (trying to manually reverse vector)

I'm trying to create a function which takes in a vector and simply reverses (manually). 我正在尝试创建一个函数,该函数接受一个向量并简单地反转(手动)。 I'm aware of the existence of reverse(), but I ran into the "Vector iterator not dereferencable" problem and for educational purposes, I'd like to know what it means. 我知道reverse()的存在,但是遇到了“向量迭代器不可引用”问题,出于教育目的,我想知道它的含义。 I tried researching this problem and someone (on this forum) said that vect.end() is not dereferencable by definition, but from my understanding, using reverse_iterator is just reversing the ends, so following the logic; 我试图研究这个问题,有人(在这个论坛上)说vect.end()在定义上不是可引用的,但是据我的理解,使用reverse_iterator只是颠倒了两端,因此遵循逻辑; vect.rend should not be dereferencable. vect.rend不应被取消引用。

vector<int> reverseVector(vector<int>);

int main()
{
    vector<int> vec;

    for (int i = 0; i < 11; i++)
    {
        vec.push_back(i);
    }

    vec = reverseVector(vec);

    for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

vector<int> reverseVector(vector<int> vect) 
{
    vector<int>::reverse_iterator ritr;
    for (ritr = vect.rbegin(); ritr != vect.rend(); ritr++)
    {
        vect.insert(vect.begin(), *ritr);
        vect.pop_back();
    }
    return vect;
}

Your problem has nothing to do with the dereferencability or otherwise of rend() . 您的问题与rend()rend()引用性无关。 You're modifying the vector while iterating over it, which invalidates the iterator. 您正在迭代时修改向量,这会使迭代器无效。

To answer your original question, a reverse_iterator isn't just "reversing the ends" compared to a forward iterator. 为了回答您的原始问题,与前向迭代器相比, reverse_iterator不仅是“使目标reverse_iterator ”。 rbegin() is end() - 1 , and rend() is begin() - 1 . rbegin()end() - 1rend()begin() - 1

If you add an element to the vector, ritr may be invalidated thus the error 如果将元素添加到向量, ritr可能会失效,从而导致错误

Vector iterator not dereferencable. 向量迭代器不可取。

Thus its better using an index as your loop variable or better a copy(temp) vector for reverse task. 因此,最好使用索引作为循环变量或更好的用于反向任务的copy(temp)向量。

You are deleting elements from the vector (popping from the back), which invalidates the reverse iterator. 您正在从向量中删除元素(从背面弹出),这会使反向迭代器无效


You could just iterate through half of the vector and swap the elements, lke this: 您可以遍历向量的一半并交换元素,例如:

void swap(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}

vector<int> reverseVector(vector<int> vect) {
    const size_t origin_size = vect.size();
    for(size_t i = 0; i < origin_size/2; ++i)
        swap(vect[i], vect[origin_size - 1 - i]);
    return vect;
}

insertpop_back成员函数都修改了向量并使迭代器无效。

A tip as design-issue: use always const-reference in a function, unless you really know what you are doing. 作为设计问题的一个提示:除非您真的知道自己在做什么,否则始终在函数中使用const-reference。 So you will avoid stepping in traps like this. 因此,您将避免陷入此类陷阱。 For example: 例如:

vector<int> reverseVector(const vector<int> &vect)

Now you wont have this problem, because you can not modify vect. 现在您将不会遇到此问题,因为您无法修改vect。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM