簡體   English   中英

程序崩潰,出現“ std :: out_of_range”錯誤

[英]Program crashing with 'std::out_of_range' error

我正在為學校進行一個命運之輪項目,並且遇到一些指針問題。

這是我的程序遇到的問題,(cmd輸出):

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::compare: __pos (which is 1) > this->size() (which is 0)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

該游戲的設計類似於運氣游戲。 首先,我要做的是過濾掉“ rlstne”字母。 無需使用指針即可工作,但我必須使用指針。 這是我的完整程序:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <cctype>
#include <time.h>
#include <Windows.h>

int main(){

    std::string original_string = "THIS LINE IS THE GAME";
    std::string str1;
    std::string str2 = "RLSTNE";
    int y = 0;

    bool restart = true;

    std::string* arr_temp =  new std::string[100];
    std::string* arr_temp1 = new std::string[100];
    *arr_temp1 = str1;
    *arr_temp = str2;
do{

        std::cout << "What string?" << std::endl;
        getline(std::cin, str1);
        std::cout << str1.length() << std::endl;

    for(int x = 0; x < str1.length(); x++){

        if((arr_temp->compare(0,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(1,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(2,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(3,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }   
        if((arr_temp->compare(4,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(5,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
}
*arr_temp1 = str1;
std::cout << *arr_temp1 <<std::endl;
Sleep(1000);
}while(restart);
}

我認為這是我的程序出錯的地方:

std::string str1;
std::string str2 = "RLSTNE";

str1沒有初始化為任何值,因此編譯器將其視為0長度,但是我嘗試將其初始化為其他值。 如original_string的字符串值。

這是代碼:

std::string str1 = "THIS LINE IS THE GAME";
std::string str2 = "RLSTNE";

這是輸出:

What string?
THIS LINE IS THE GAME
21
_HI_ _I__ I_ _H_ GAM_

但是,當我嘗試添加比原始值21更多的值時,出現此問題:

What string?
THIS LINE IS THE GAMEEEE
24
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::compare: __pos (which is 22) > this->size() (which is 21)

所以我的問題是:編譯器輸出什么? 什么是22,什么是21? 這個->大小是什么意思? __pos是什么意思?

提前致謝。

std::string* arr_temp =  new std::string[100];
std::string* arr_temp1 = new std::string[100];

這些都是指向100個字符串數組的指針。 另外,由於使用new ,因此需要在代碼中的某處delete ,否則會發生內存泄漏。 但是您似乎不需要動態內存。 所以固定版本是

std::string* arr_temp;
std::string* arr_temp1;

您的big for循環可以使用嵌套循環進行簡化,但這並不是這里的重點。 至於您的錯誤-異常std::out_of_range表示您超出了數組的限制。 導致它的代碼如下:

std::string str1 = "THIS LINE IS THE GAME"; //makes a new string
*arr_temp1 = str1; //dereferences arr_temp1 and sets arr_temp1[0] to whatever str1 currently is

因此,您已經設置了arr_temp1[0] = "THIS LINE IS THE GAME" (長度為21)。 然后設置str1 = "THIS LINE IS THE GAMEEEE" (長度為24)。 您的循環嘗試訪問arr_temp1 [0]的前24個字符。 但這是行不通的-它的長度為21。因此,一旦到達第22個字符,就會引發std::out_of_range錯誤。

總而言之,大多數操作並沒有按照您的想法進行。 我建議先閱讀一下指針,然后從頭開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM