向量分割错误

时间:2018-10-16 12:04:55

标签: c++ vector

我正在做的程序未完成for循环,并且在循环结束之前抛出了细分错误。我可以问一下是否是因为循环太小了吗?

for(vector<string>::iterator it = passList.begin(); it!= passList.end();it++)
    {
        for (size_t x = 0; x < passList.size()-1; ++x)
        {
            while(passList[x] == used)
            {
                x++;
            }
                firstPW = passList[x];
                cout << firstPW << endl;
                //passList.erase(remove(passList.begin(), passList.end(), firstPW), passList.end());
                replace (passList.begin(), passList.end(), firstPW, used);
                chainHead.push_back(firstPW);
                strmd5 = MD5Hash(firstPW);
                //chainTail.push_back(strmd5);
                //cout << strmd5 << endl;

                strmd5.erase(remove_if(strmd5.begin(), strmd5.end(), isnonnum), strmd5.end());
                cout << strmd5 << endl;

                numOfDigits = strmd5.length();
                //cout << numOfDigits << endl;

                digitsToMinus = numOfDigits - to_string(count).length();
                strmd5.erase(strmd5.length()-digitsToMinus);
                cout << strmd5 << endl;

                intmd5 = stoi(strmd5);
                //cout << intmd5 << endl;
                intmd5 = intmd5 + 1;
                index = intmd5 % count;
                cout << index << endl;

                chainHead2.push_back(chainHead[0]);
        }

        for(int i=0; i<3; i++)
        {
            firstPW = passList2[index];
            //cout << firstPW << endl;
            //passList.erase(remove(passList.begin(), passList.end(), firstPW), passList.end());
            replace (passList.begin(), passList.end(), firstPW, used);
            //chainHead.push_back(firstPW);
            strmd5 = MD5Hash(firstPW);
            chainTail.push_back(strmd5);
            //cout << strmd5 << endl;

            strmd5.erase(remove_if(strmd5.begin(), strmd5.end(), isnonnum), strmd5.end());
            //cout << strmd5 << endl;

            numOfDigits = strmd5.length();
            //cout << numOfDigits << endl;

            digitsToMinus = numOfDigits - to_string(count).length();
            strmd5.erase(strmd5.length()-digitsToMinus);
            //cout << strmd5 << endl;

            intmd5 = stoi(strmd5);
            //cout << intmd5 << endl;
            //intmd5 = intmd5 + 1;
            index = intmd5 % count;
            //cout << index << endl;
            if(i = 3)
            {
                temp = chainTail.back();
                chainTail2.push_back(temp);
            } 
        }
        chainHead.clear();
        chainTail.clear();
        /*for(int a=0; a<chainHead2.size(); a++)
        {
            cout << chainHead2[a] << endl;
        }
        for(int b=0; b<chainTail2.size(); b++)
        {
            cout << chainTail2[b] << endl;
        }*/
        PWchain = chainHead2[0];
        HASHchain = chainTail2[0];
        chainHead2.clear();
        chainTail2.clear();

        Rainbowtxt.open("Rainbow.txt",ios::app);

        Rainbowtxt << PWchain << " " << HASHchain << endl;
        Rainbowtxt.close();
    }

我尝试过更改变量名,以免它们冲突,但错误仍然相同。我也感谢任何改进我的代码的建议。

1 个答案:

答案 0 :(得分:0)

在以下代码中:

for (size_t x = 0; x < passList.size()-1; ++x)
{
    while(passList[x] == used)
    {
        x++;
    }

在while循环内递增x的值后,您没有边界检查,这很可能导致访问passList边界之外的项目。