为什么VecMedian会超出范围?

时间:2022-04-30 16:43:46

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:

目前我正在试图弄清楚为什么我的辅助函数用于计算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);

}

Thank you.

3 个解决方案

#1


0  

Consider your median function

考虑你的中位数函数

 T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

If the input arg size is 2 .. you can only access arg[0] and arg[1];

如果输入arg大小为2 ..您只能访问arg [0]和arg [1];

your medians will be 2 and thus out of range

你的中位数将是2,因此超出范围

#2


0  

template<typename T>
T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

. Suppose you have an empty vector. medians will be 0 + 1 and you'll try to access index 1 for an empty vector.

。假设你有一个空向量。中位数将为0 + 1,您将尝试访问索引1以获取空向量。

Add check like

添加支票就好

if( medians < arg.size() )
//..

The same would happen for size 2, for example.

例如,对于尺寸2,也会发生相同的情况。

#3


0  

return arg.at(medians);

Most likely exception is thrown in this line of code, when medians becomes invalid index for std::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.

当medians成为std :: vector的无效索引时,很可能在这行代码中抛出异常。您可以在gdb中捕获此异常。加载程序并为此类(gdb)catch throw之类的异常设置catchpoint。这将有助于进一步调试问题。

#1


0  

Consider your median function

考虑你的中位数函数

 T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

If the input arg size is 2 .. you can only access arg[0] and arg[1];

如果输入arg大小为2 ..您只能访问arg [0]和arg [1];

your medians will be 2 and thus out of range

你的中位数将是2,因此超出范围

#2


0  

template<typename T>
T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

. Suppose you have an empty vector. medians will be 0 + 1 and you'll try to access index 1 for an empty vector.

。假设你有一个空向量。中位数将为0 + 1,您将尝试访问索引1以获取空向量。

Add check like

添加支票就好

if( medians < arg.size() )
//..

The same would happen for size 2, for example.

例如,对于尺寸2,也会发生相同的情况。

#3


0  

return arg.at(medians);

Most likely exception is thrown in this line of code, when medians becomes invalid index for std::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.

当medians成为std :: vector的无效索引时,很可能在这行代码中抛出异常。您可以在gdb中捕获此异常。加载程序并为此类(gdb)catch throw之类的异常设置catchpoint。这将有助于进一步调试问题。