gpt4 book ai didi

c++ - 运行非常简单的程序 (C++) 时出现运行时错误

转载 作者:行者123 更新时间:2023-11-28 00:28:34 24 4
gpt4 key购买 nike

<分区>

作为学习练习的一部分,我编写了一个程序,该程序将 double 作为输入,并输出所有 double 的总和,以及最小值、最大值和平均值。尝试运行该程序时,出现此运行时错误:

 Unhandled exception at 0x74b01d4d in Hello World.exe: Microsoft C++ exception: Range_error at memory location 0x00dcf760.

当我使用 Visual Studio 2010 调试选项时,我得到 vector subscript is out of range

代码是:

#include "C:\Users\Kevin\Documents\Visual Studio 2010\Projects\std_lib_facilities.h"

int main() {
vector<double> v;
double number = 0;
cout << "Enter the distance between two cities along the route.\n";
while (cin >> number) {
double sum = 0;
v.push_back(number);
sort(v.begin(),v.end());
for (size_t i = 0; i < v.size(); ++i)
sum += v[i];
cout << "The total distance from each city is " << sum
<< ". The smallest distance is " << v[0]
<< " and the greatest distance is " << v[v.size()]
<<". The mean distance is " << sum/v.size() <<".\n";
cout << "Enter the distance between two cities along the route.\n";}
}

我不得不更改 for 循环中定义的变量类型,因为我遇到了有符号/无符号不匹配错误。代码在编译时没有给出错误,我很难看到问题所在。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com