C++进程以状态 -1073741819 终止

C++ Process terminated with status -1073741819

本文关键字:-1073741819 终止 状态 进程 C++      更新时间:2023-10-16

我正在创建一个小字典。我创建了一个字符串向量来预先打印一些单词,以将其中一个作为用户的输入并向他们描述单词。

我尝试在谷歌上搜索它,并试图在 for 循环中设置unsigned int i = 0

下面给出了这样做的代码部分:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    vector<string> word = {"none", "jump fatigue" , "scrim game", "box up", "turtling", "swing", "flickshot", "tracking", "panic build", "cone jump", "ttv", "one-shot", "tagged", "blue", "white", "lasered", "melted", "default", "bot", "stealth", "aggresive", "sweaty", "tryhard", "choke"};
    for(int i = 0; i <= word.size(); i++){
        cout<<i<<")"<< word[i] << endl;
    }
    return 0;
}

它打印没有任何错误,在运行代码结束时,它会冻结一段时间并结束, Process terminated with status -1073741819(0 minute(s), 4 second(s))而它应该以 0 终止

在调试代码时,我得到 warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

您的问题出在 for 循环i <= word.size() 中。这应该是<.最后一个索引将比大小小 1,因为第一个索引为 0。

我建议至少在 for 循环中使用 size_t 以获得更好的类型

for (std::size_t i = 0; i < word.size(); i++) {

尽管更简洁的迭代方式是基于范围的 for 循环

for (auto& w : word) {
    std::cout << w << 'n';
}