繁体   English   中英

C ++在运行时“向量迭代器不可引用”

[英]C++ “vector iterator not dereferencable” in runtime

在执行此简单程序时,我在运行时发现了在FIRST while末尾键入eof时发生的错误。 错误显示“ 向量迭代器不可解除 ”。 那可能吗? 迭代器在while循环之后声明! 我仍然不明白我拼错了什么。 有人能帮我吗? 附言 程序应检查向量是否是另一个的前缀。 谢谢! 主要功能如下:

   int main(){
        vector<int> v1, v2;
        cout << "1st vector: enter integers" << endl;
        int i, j;
        while (cin >> i){
            v1.push_back(i);
        }
        cout << "2nd vector: enter integers" << endl;
        while (cin >> j){
            v2.push_back(j);
        }
        vector<int>::const_iterator i1 = v1.begin();
        vector<int>::const_iterator i2 = v2.begin();
        while ((*i1) && (*i2) && ((*i1) == (*i2))){
            ++i1; ++i2;
        }
        if ((*i1) && (*i2))
            cout << "Vectors not compatible." << endl;
        else
        if (!(*i1))
            cout << "The 1st vector is a prefix for the 2nd." << endl;
        else
            cout << "The 2nd vector is a prefix for the 1st." << endl;
        return 0;
    }

vector<int>不是以N结尾的c样式字符串。 因此,要检查迭代器是否到达末尾,需要将其与末尾迭代器进行比较。 因此,在两种情况下都应该写(i1 != v1.end()) && (i2 != v2.end())而不是(*i1) && (*i2) !(*i1) 您应该将其更改为i1 == v1.end()

@RSahu描述了您遇到的第一个问题。 修复第一个问题后,您将遇到我描述的问题。

要解决他描述的问题,您应该清除不良位,并忽略cin缓冲区中剩余的内容。 在您的第二个while循环之前添加以下行:

cin.clear();
cin.ignore();

生活

一旦您在cin上获得了第一个eof,它就会保留在那里。 由于cin认为已完成,因此第二个while循环实际上变成了无事。 从那里开始,我们进行以下操作:

vector<int>::const_iterator i1 = v1.begin();
vector<int>::const_iterator i2 = v2.begin();
while ((*i1) && (*i2) && ((*i1) == (*i2))){
                 ^^^
                 UB!

并且您在不检查v2大小的情况下取消引用i2

首先,您必须清除std::cin

std::cin.clear();

然后,检查向量是否正确的正确方法是将迭代器与end()进行比较(与简单地取消引用相反):

while (i1 < v1.end() && i2 < v2.end() && *i1 == *i2) {
    ++i1;
    ++i2;
}

虽然如果可以访问C ++ 14编译器,则可以简单地使用std::mismatch

auto res = std::mismatch(v1.begin(), v1.end(), v2.begin(), v2.end());
if (res.first < v1.end()) {
    if (res.second < v2.end()) {
        std::cout << "Not compatible!" << std::endl;
    }
    else {
        std::cout << "The 2nd vector is a prefix for the 1st." << std::endl;
    }
}
else {
    std::cout << "The 1st vector is a prefix for the 2nd." << std::endl;
}

以下块:

    while (cin >> i){
        v1.push_back(i);
    }

确保cin >> j失败。 因此,该块中没有任何内容添加到v2

    while (cin >> j){
        v2.push_back(j);
    }

由于v2为空,因此使用*i2会导致未定义的行为。

暂无
暂无

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

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