【问题标题】:std::out_of_range for a C++ polynomial class using std::vector<int>使用 std::vector<int> 的 C++ 多项式类的 std::out_of_range
【发布时间】:2018-07-23 20:46:56
【问题描述】:

我是 C++ 的新手,并使用 std::vector 编写了一个多项式类。一切正常,直到我尝试调用函数 getCoeff(int index) 应该返回特定索引处的系数。在我的情况下,getCoeff(0) 应该返回第 0 个系数,即“1”。

我在使用 g++ 编译时收到此错误:

terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >=this-> size() (which is 0)
Aborted

多项式.h:

#include<iostream>
#include<vector>

using namespace std;

class Polynomial
{
    public:
    Polynomial(int deg, std::vector<int> coeff);
    Polynomial(const Polynomial & p);
    ~Polynomial();
    const int getCoeff(int index);

    private:
    int degree;
    std::vector<int> coefficient;

};

Polynomial::Polynomial(int deg, std::vector<int> coeff)
{
    degree = deg;
    std::vector<int> coefficient(deg);
    for( int i = degree; i >= 0; i--) 
        coefficient[i] = coeff[i];
}

Polynomial::Polynomial(const Polynomial & p)
{
    degree=p.degree;
    std::vector<int> coefficient(p.degree);
    for ( int i=p.degree; i>= 0; i-- )
        coefficient[i] = p.coefficient[i];
}

const int Polynomial::getCoeff(int index)
{
    return coefficient[index];
}

Polynomial::~Polynomial()
{
    //delete[] & coefficient;
    coefficient.clear();
}

还有主文件,我在其中创建了一个测试多项式 test1,它的次数为 3,系数为 1,9,3,4(注意:std::vector coeff1 是由数组 ko1[ ] ):

int main ()
{
    int ko1[] = {1,9,3,4};
    int degree1 = sizeof(ko1)/sizeof(*ko1)-1;

    std::vector<int> coeff1;
    for (int i=0; i<= degree1; i++)
        coeff1.push_back(ko1[i]);

    Polynomial test1(degree1, coeff1);

    cout << "Coefficients: " << endl;
    for (int j=0; j<=degree1; j++)
    cout << coeff1[j] << endl;


    cout << test1.getCoeff(0); //this is where the error occurs

    return 0;

}

我怀疑我的构造函数有错误,或者从数组中获取的元素在新的std::vector中不被接受谢谢你的帮助。

【问题讨论】:

  • 不相关,但你知道std::vector 有复制构造函数和赋值运算符,对吧?您无需逐个复制元素。
  • @StoryTeller OP 必须使用反向迭代器,而不是直接复制结构。此外,dtor 中的.clear() 是不必要的。
  • @Borgleader - 很公平。但即便如此,std::vector 的迭代器接受 c'tor 以及 assign 成员也应该这样做。它会看起来更干净,IMO。
  • @Borgleader - 实际上,源向量的每个元素都被复制到目标向量中的相应位置。为什么不简单的分配或复制呢?

标签: c++ c++11 vector


【解决方案1】:

您的代码中经常出现的一个错误是它假定向量具有索引为[0, size] 的元素,而它是一个半开范围[0, size)。例如。构造函数中的那些循环实际上应该是 for(int i = degree; i-- &gt; 0;)for(int i = 0; i &lt; degree; ++i) 以避免访问索引 degree 处不存在的元素。

另一个是构造函数Polynomial(int deg, std::vector&lt;int&gt; coeff)未能初始化coefficient成员变量,而是初始化了一个局部变量。修复:

Polynomial::Polynomial(int deg, std::vector<int> const& coeff)
    : degree(deg)
    , coefficient(coeff)
{}

因为degree是系数的个数,你可以去掉成员degree,用coefficient.size()代替它。

代码不需要自定义复制构造函数或析构函数,您可以安全地删除它们,或者在必要时将它们声明为= default

【讨论】:

  • 我不太确定第一部分。 OP push_backs 元素使用与迭代相同的条件。
  • @Borgleader 查看构造函数中的循环。
  • @MaximEgorushkin 这也说对了一半。看到 ctor Polynomial test1(degree1, coeff1); 的调用,而 degree1 是 int degree1 = sizeof(ko1)/sizeof(*ko1)-1; 但是,你是对的,在课堂上使用 [0, size] 会产生误导......
  • @MaximEgorushkin 非常感谢,它现在可以正常工作了。但是,我不明白为什么,这意味着我不知道“:度(度),系数(系数)”部分是什么意思。
  • @Retze 即初始化列表语法,见en.cppreference.com/w/cpp/language/initializer_list
猜你喜欢
  • 2022-01-03
  • 2020-01-26
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多