c++ - 程序崩溃并出现 'std::out_of_range' 错误

标签 c++

我正在为学校做一个命运之轮项目,遇到了一些关于指针的问题。

这是我的程序的问题,(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 是什么意思?

提前致谢。

最佳答案

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 错误。

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

关于c++ - 程序崩溃并出现 'std::out_of_range' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110683/

相关文章:

c++ - 如何制作 shared_ptr 列表?

c++ - 有没有办法让 gcc 或 clang 在缺少 'else' 时发出警告?

c++ - CMake 库目标并不总是出现在 Visual Studio 2017 的启动项下拉列表中

c++ - 不同类别的 2 个对象之间的信号/插槽

c++ - 迭代集合而不首先生成集合

C++ 标准库 : Achieving constant time remove from linked list

c++ - 切换到 MPI_LONG_LONG_INT 会崩溃吗?

c++ - Lambda 捕获导致不兼容的操作数类型错误?

c++ - 用于从汇编代码中提取 API 调用序列和控制流图的开源代码

c++ - 从 std::stringstream 检索 char