В настоящее время я пытаюсь выяснить, почему моя вспомогательная функция для вычисления медианы вектора чисел HW работает неправильно. Моя вспомогательная функция должна работать не только с вектором.
Ошибка:
./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);
}
Спасибо.