简体   繁体   English

字符串流问题-向量迭代器不可取消

[英]stringstream problem - vector iterator not dereferencable

I've got a problem with the following code snippet. 以下代码段存在问题。

It is related to the stringstream "stringstream css(cv.back())" bit. 它与字符串流“ stringstream css(cv.back())”位有关。 If it is commented out the program will run ok. 如果被注释掉,程序将正常运行。

It is really weird, as I keep getting it in some of my programs, but if I just create a console project the code will run fine. 确实很奇怪,因为我在某些程序中不断得到它,但是如果我只创建一个控制台项目,则代码可以正常运行。 In some of my Win32 programs it will and in some it won't (then it will return "vector iterator not dereferencable" but it will compile just fine). 在我的某些Win32程序中,它会(在某些情况下不会)(然后它将返回“不可迭代的矢量迭代器”,但可以正常编译)。

Any ideas at all would be really appreciated. 任何想法都将不胜感激。 Thanks! 谢谢!

vector<double> cRes(2);
vector<double> pRes(2);

int readTimeVects2(vector<double> &cRes, vector<double> &pRes){
    string segments;
    vector<string> cv, pv, chv, phv;
    ifstream cin("cm.txt");
    ifstream pin("pw.txt");
    ifstream chin("hm.txt");
    ifstream phin("hw.txt");

    while (getline(cin,segments,'\t')) {
        cv.push_back(segments);
    }

    while (getline(pin,segments,'\t')) {
        pv.push_back(segments);
    }

    while (getline(chin,segments,'\t')) {
        chv.push_back(segments);
    }

    while (getline(phin,segments,'\t')) {
        phv.push_back(segments);
    }

    cin.close();  
    pin.close();  
    chin.close();   
    phin.close();

    stringstream phss(phv.front());
    phss >> pRes[0];
    phss.clear();
    stringstream chss(chv.front());
    chss >> cRes[0];
    chss.clear();

    stringstream pss(pv.back());
    pss >> pRes[1];
    pss.clear();
    stringstream css(cv.back());
    css >> cRes[1];
    css.clear();

    return 0;
}

There are two major issues here. 这里有两个主要问题。 Either or both of these problems can be causing the issue you are experiencing. 这些问题中的一个或两个都可能导致您遇到问题。

You are hiding names from outside your scope : 您正在隐藏范围之外的名称

vector<double> cRes(2);
vector<double> pRes(2);

int readTimeVects2(vector<double> &cRes, vector<double> &pRes){

cRes and pRes are going to be the variables passed to your function, not the global variables you have demonstrated. cRes和pRes将是传递给函数的变量,而不是您已演示的全局变量。

You need to show us the calling code where the issue occurs before we will be able to diagnose this problem -- though I wonder why you're not just using push_back here, 您需要向我们显示发生问题的调用代码,然后我们才能诊断出此问题-尽管我想知道为什么您不仅仅在这里使用push_back,


There's another problem here: 这里还有另一个问题:

stringstream phss(phv.front());
stringstream chss(chv.front());
stringstream pss(pv.back());
stringstream css(cv.back());

You have no checking to ensure phv, chv, pv, and cv are not empty . 您没有检查以确保phv,chv,pv和cv不为空 It's entirely possible your std::getline s above had problems that prevented them from completing successfully, and it's also entirely possible that the files you passed were empty. 上面的std::getline很有可能遇到问题,导致无法成功完成,并且传递的文件也很可能为空。

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

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