为什么VecMedian会超出范围?

时间:2011-11-21 07:24:44

标签: c++ templates vector outofrangeexception

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

}

谢谢。

3 个答案:

答案 0 :(得分:0)

考虑你的中位数函数

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

}

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

您的medians 2 因此超出范围

答案 1 :(得分: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);

}

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

添加支票

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

例如,对于大小2也会发生同样的情况。

答案 2 :(得分:0)

return arg.at(medians);

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