-2

スタッカーの皆様、良い一日を。Quincy2005 でプログラムを実行していますが、次のエラーが発生します。「'std::out_of_range のインスタンスをスローした後に呼び出されて終了します」「what(): vector::_M_range_check」

以下は私のコードの束です

int ptextLoc,ctextLoc;       //location of the plain/cipher txt
char ctextChar;     //cipher text variable
//by default, the location of the plain text is even
bool evenNumberLocBool = true;
ifstream ptextFile;
//open plain text file
ptextFile.open("ptext.txt");

    //character by character encryption
    while (!ptextFile.eof())
    {

        //get (next) character from file and store it in a variable ptextChar
        char ptextChar = ptextFile.get();

        //find the position of the ptextChar in keyvector Vector
        ptextLoc = std::find(keyvector.begin(), keyvector.end(), ptextChar) - keyvector.begin();

        //if the location of the plain text is even
        if (  ((ptextLoc % 2) == 0) || (ptextLoc == 0) )
            evenNumberLocBool = true;
        else
            evenNumberLocBool = false;

        //if the location of the plain text is even/odd, find the location of the cipher text    
        if (evenNumberLocBool)
            ctextLoc = ptextLoc + 1;
        else
            ctextLoc = ptextLoc - 1;


        //store the cipher pair in ctextChar variable
        ctextChar = keyvector.at(ctextLoc);

        cout << ctextChar;

    }

ptext.txt ab cd ef の内容

最初の文字が 0 の位置にある 'a' の場合、ペア暗号アルファベットは kevector[1] になります。

最新の更新: このエラーを作成している行を見つけました。ctextChar = keyvector.at(ctextLoc); ただし、なぜこの行で発生しているのかはわかりません。誰かが私を案内してくれることを願っています。

4

2 に答える 2

3

std::vector'sは値をsize()返しますが1 --- N、as は値にat()依存します0 --- (N - 1)。したがって、次を使用する必要があります。

if (keyvector.size() != 0 && ctextLoc > keyvector.size() - 1)
  break;  
于 2012-07-26T13:37:08.733 に答える
3
if (ctextLoc > keyvector.size())

おそらくあるはずです

if (ctextLoc >= keyvector.size())
于 2012-07-26T13:38:20.203 に答える