【问题标题】:C++ Process terminated with status -1073741819C++ 进程以状态 -1073741819 终止
【发布时间】:2019-06-07 04:34:57
【问题描述】:

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

我尝试在谷歌上搜索并尝试在 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]

【问题讨论】:

  • i &lt;= 应该是 i &lt;,您的最后一次循环迭代读取到向量的末尾。或者更好的是,使用基于范围的 for 循环来避免这种错误。

标签: c++ c++11


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 2017-03-26
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多