首页 > 解决方案 > 程序因“std::out_of_range”错误而崩溃

问题描述

我正在为学校做一个幸运之轮项目,并且遇到了一些指针问题。

这是我在程序中遇到的问题(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是什么值?this->size 是什么意思?__pos 是什么意思?

提前致谢。

标签: c++

解决方案


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;

你的大 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错误。

总而言之,其中大部分都没有按照您的想法进行。我建议稍微阅读一下指针并从头开始。


推荐阅读