简体   繁体   English

代码中的“ std :: out_of_range”错误

[英]'std::out_of_range' error from code

The following error message was received after running my code located at the end of the message: 运行位于消息末尾的代码后,收到以下错误消息:

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) 抛出'std :: out_of_range'实例之后的终止调用what():vector :: _ M_range_check:__n(0)> = this-> size()(0)

This application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact the application's support team for more information. 请与应用程序的支持团队联系以获取更多信息。

I'm sorry for the length of the code. 我很抱歉代码的长度。 It appears that the error is coming from when I am calling the numerov function within the f function. 当我在f函数中调用numerov函数时,似乎出现了错误。 If you are able to determine what the error is would you please let me know? 如果您能够确定错误的原因,请告诉我? Thank you! 谢谢!

#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>

using namespace std;

int nx = 500, m = 10, ni = 10;
double x1 = 0, x2 = 1, h = (x2 - x1)/nx;
int nr, nl;
vector<double> ul, q, u;


//Method to achieve the evenly spaced Simpson rule
double simpson(vector <double> y, double h)
{
    int n = y.size() - 1;
    double s0 = 0, s1 = 0, s2 = 0;
    for (int i = 1; i < n; i += 2)
    {
        s0 += y.at(i);
        s1 += y.at(i-1);
        s2 += y.at(i+1);
    }
    double s = (s1 + 4*s0 + s2)/3;

//Add the last slice separately for an even n+1
if ((n+1)%2 == 0)
    return h*(s + (5*y.at(n) + 8*y.at(n-1) - y.at(n-2))/12);
else
    return h*2;
}

//Method to perform the Numerov integration
vector <double> numerov(int m, double h, double u0, double u1, double q)
{
    vector<double> u;
    u.push_back(u0);
    u.push_back(u1);
    double g = h*h/12;
    for (int i = 1; i < m+1; i++)
    {
        double c0 = 1 + g*q;
        double c1 = 2 - 10*g*q;
        double c2 = 1 + g*q;
        double d = g*(0);
        u.push_back((c1*u.at(i) - c0*u.at(i-1) + d)/c2);
    }
    return u;
}

//Method to provide the function for the root search
double f(double x)
{
    vector<double> w;
    vector<double> j = numerov(nx + 1, h, 0.0, 0.001, x);
    for (int i = 0; i < 0; i++)
    {
        w.push_back(j.at(i));
    }
    return w.at(0);
}

//Method to carry out the secant search
double secant(int n, double del, double x, double dx)
{
    int k = 0;
    double x1 = x + dx;
    while ((abs(dx) > del) && (k < n))
    {
        double d = f(x1) - f(x);
        double x2 = x1 - f(x1)*(x1 - x)/d;
        x = x1;
        x1 = x2;
        dx = x1 - x;
        k++;
    }
    if (k == n)
        cout << "Convergence not found after " << n << " iterations." <<                 endl;
    return x1;
}


int main()
{
    double del = 1e-6, e = 0, de = 0.1;

    //Find the eigenvalue via the secant method
    e = secant (ni, del, e, de);

    //Find the solution u(x)
    u = numerov(nx + 1, h, 0.0, 0.01, e);

    //Output the wavefunction to a file
    ofstream myfile ("Problem 2.txt");
    if (myfile.is_open())
    {
        myfile << "Input" << "\t" << "u(x)" << endl;
        double x = x1;
        double mh = m*h;
        for (int i = 0; i <= nx; i += m)
        {
            myfile << x << "\t" << u.at(i) << endl;
            x += mh;
        }
        myfile.close();
    }

    return 0;
}
vector<double> w;
for (int i = 0; i < 0; i++)
{
    w.push_back(j.at(i));
}
return w.at(0);

w will have nothing in it, since that loop will run 0 times. w将没有任何内容,因为该循环将运行0次。 Thus, w.at(0) will throw the out of range error. 因此, w.at(0)将抛出超出范围的错误。

Why do you think the problem is in the numerov function? 您为什么认为问题出在numerov函数中?

I see an error in the function f ? 我在函数f看到错误?

vector<double> w;
vector<double> j = numerov(nx + 1, h, 0.0, 0.001, x);
for (int i = 0; i < 0; i++)
{
    w.push_back(j.at(i));
}
return w.at(0);

There is nothing on vector w and you try to access element 0. 向量w上没有任何内容,您尝试访问元素0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM