为什么 VecMedian 生成超出范围?
目前,我正在尝试找出为什么我的用于计算硬件数字向量中位数的辅助函数无法正常工作。我的辅助函数应该不仅仅适用于向量。 错误:
./a.out
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
AVG: 30Aborted
Student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
Student(string tname);
double getHWWeight();
void setHWWeight(double arg);
double getmidtermExamWeight();
void setmidtermExamWeight(double arg);
double getfinalExamWeight();
void setfinalExamWeight(double arg);
double getmidterm();
void setmidterm(double arg);
double getfinal();
void setfinal(double arg);
void addHW(double arg);
void readHW(istream &i);
double getHWAverage();
double getHWMedian();
private:
string name;
double midterm;
double final;
vector<double> HW;
static double HWWeight;
static double midtermExamWeight;
static double finalExamWeight;
};
#endif
Student.cpp:
#include "Student.h"
#include <algorithm>
double Student::HWWeight = 60;
double Student::midtermExamWeight = 15;
double Student::finalExamWeight = 25;
template<typename T>
T VecAverage(vector<T> arg){
typename vector<T>::iterator it;
T temp=0;
for(it=arg.begin(); it < arg.end(); it++)temp+=*it;
temp/=arg.size();
return temp;
}
template<typename T>
T VecMedian(vector<T> arg){
int medians = arg.size()/2;
medians+=1;
sort(arg.begin(),arg.end() );
return arg.at(medians);
}
Student::Student(string tname){
name = tname;
}
double Student::getHWWeight(){
return HWWeight;
}
void Student::setHWWeight(double arg){
HWWeight = arg;
}
double Student::getmidtermExamWeight(){
return midtermExamWeight;
}
void Student::setmidtermExamWeight(double arg){
midtermExamWeight = arg;
}
double Student::getfinalExamWeight(){
return finalExamWeight;
}
void Student::setfinalExamWeight(double arg){
finalExamWeight = arg;
}
double Student::getmidterm(){
return midterm;
}
void Student::setmidterm(double arg){
midterm = arg;
}
double Student::getfinal(){
return final;
}
void Student::setfinal(double arg){
final = arg;
}
void Student::addHW(double arg){
HW.push_back(arg);
}
void Student::readHW(istream &i){
int x;
i >> x ;
while(x >= 0){
HW.push_back(x);
i >> x;
}
}
double Student::getHWAverage(){
return VecAverage(HW);
}
double Student::getHWMedian(){
return VecMedian(HW);
}
谢谢。
Currently I'm trying to figure out why my helper function for computing the median of a vector of HW numbers is not working correctly. My helper function is suppose to work with more than just a vector.
Error:
./a.out
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
AVG: 30Aborted
Student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
Student(string tname);
double getHWWeight();
void setHWWeight(double arg);
double getmidtermExamWeight();
void setmidtermExamWeight(double arg);
double getfinalExamWeight();
void setfinalExamWeight(double arg);
double getmidterm();
void setmidterm(double arg);
double getfinal();
void setfinal(double arg);
void addHW(double arg);
void readHW(istream &i);
double getHWAverage();
double getHWMedian();
private:
string name;
double midterm;
double final;
vector<double> HW;
static double HWWeight;
static double midtermExamWeight;
static double finalExamWeight;
};
#endif
Student.cpp:
#include "Student.h"
#include <algorithm>
double Student::HWWeight = 60;
double Student::midtermExamWeight = 15;
double Student::finalExamWeight = 25;
template<typename T>
T VecAverage(vector<T> arg){
typename vector<T>::iterator it;
T temp=0;
for(it=arg.begin(); it < arg.end(); it++)temp+=*it;
temp/=arg.size();
return temp;
}
template<typename T>
T VecMedian(vector<T> arg){
int medians = arg.size()/2;
medians+=1;
sort(arg.begin(),arg.end() );
return arg.at(medians);
}
Student::Student(string tname){
name = tname;
}
double Student::getHWWeight(){
return HWWeight;
}
void Student::setHWWeight(double arg){
HWWeight = arg;
}
double Student::getmidtermExamWeight(){
return midtermExamWeight;
}
void Student::setmidtermExamWeight(double arg){
midtermExamWeight = arg;
}
double Student::getfinalExamWeight(){
return finalExamWeight;
}
void Student::setfinalExamWeight(double arg){
finalExamWeight = arg;
}
double Student::getmidterm(){
return midterm;
}
void Student::setmidterm(double arg){
midterm = arg;
}
double Student::getfinal(){
return final;
}
void Student::setfinal(double arg){
final = arg;
}
void Student::addHW(double arg){
HW.push_back(arg);
}
void Student::readHW(istream &i){
int x;
i >> x ;
while(x >= 0){
HW.push_back(x);
i >> x;
}
}
double Student::getHWAverage(){
return VecAverage(HW);
}
double Student::getHWMedian(){
return VecMedian(HW);
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
。假设你有一个空向量。
中位数
将为0 + 1
,您将尝试访问空向量的索引1
。添加检查
,例如,尺寸 2 也会发生同样的情况。
. Suppose you have an empty vector.
medians
will be0 + 1
and you'll try to access index1
for an empty vector.Add check like
The same would happen for size 2, for example.
当
medians
成为std::vector
的无效索引时,这行代码很可能会引发异常。您可以在 gdb 中捕获此异常。加载您的程序并为异常设置捕获点,例如(gdb) catch throw
。这将有助于进一步调试问题。Most likely exception is thrown in this line of code, when
medians
becomes invalid index forstd::vector
. You can catch this exception in gdb. Load your program and set catchpoint for exceptions like this(gdb) catch throw
. This will help to further debug the problem.考虑你的中值函数
如果输入arg大小是2..你只能访问arg[0]和arg[1];
您的
中位数
将为2并且因此超出范围Consider your median function
If the input arg size is 2 .. you can only access arg[0] and arg[1];
your
medians
will be 2 and thus out of range