yoci77 2019-08-15 11:05 采纳率: 0%
浏览 426

vector out of range如何预防

做了个小软件,在做异常处理,正确格式是形如“CIK2+2.22”这样的,CI可以没有,但是每次我的输入是“K+”的时候,他就会弹出vector out of range的错误弹框,试了网上很多解决的代码还是没有用,想找大佬帮着看看。

附上代码:

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
#include<vector>
#include<sstream>
#include <cmath>
#include <iomanip>


using namespace std;


class Mileage
{
public:
    int p;
    double q;
    string c;
    string mileage;
    Mileage(string);

    double getkm(void)
    {
        return p + 0.001*q;
    }

    double getm(void)
    {
        return 1000 * p + q;
    }

    double inckm(double i)
    {
        double j;
        double sum;
        j = getkm();
        sum = i + j;
        return sum;
    }

private:
    string a;
    string b;
};
Mileage::Mileage(string ss)
{
    mileage = ss;   
    vector<string>num;
    boost::split(num, mileage, boost::is_any_of("K+"), boost::token_compress_on);
    a = num[1];
    b = num[2];
    c = num[0];
    stringstream aa(a.c_str());
    aa >> p;
    stringstream bb(b.c_str());
    bb >> q;
}

double getdist(double m, double n)
{
    return n - m;
}

bool checkNum(string str)
{    
    unsigned int iSize = str.size();
    unsigned int i = 0;
    int icount = 0;
    int iCount = 0;
    for (i = 0; i <= iSize; i++)
    {
        if ('K' == str[i])
        {
            icount++;
        }
        else if ('+' == str[i])
        {
            iCount++;
        }
    }
    if (icount != 1 || iCount != 1)
    {
        cout << "Wrong! The format of chainage number is not correct. ";
        return true;
    }
    else
        return false;
}
bool compare(string m, string n)
{
    vector<string>v;
    vector<string>w;
    boost::split(v, m, boost::is_any_of("K+"), boost::token_compress_on);
    boost::split(w, n, boost::is_any_of("K+"), boost::token_compress_on);
    string sa = v[0];
    string sb = w[0];
    if (v[0] == w[0])
        return false;
    else
    {
        cout << "The crown number is not same!";
        return true;
    }
}

bool checkTh(string ss)
{
    vector<string>v;
    boost::split(v, ss, boost::is_any_of("K+"), boost::token_compress_on);
    string d = v[2];
    double ddd;
    stringstream dd(d.c_str());
    dd >> ddd;

    if (ddd >= 1000)
    {
        cout << "Wrong Chainage Number B!";
        return true;
    }
    else
        return false;
}

int main()
{
    cout << "Please enter the chainage number: ";
    string m;
    string M;
    cin >> m;

    while (checkNum(m)||checkTh(m))
    {
        cout << "Please enter the correct number:";
        cin >> m;
    }M = m;
    Mileage x(M);
    double g = x.getkm();

    char ch;
    cout << "The function of this program is: " << endl;
    cout << "A. Unified the unit of chainage number to km." << endl;
    cout << "B. Unified the unit of chainage number to m." << endl;
    cout << "C. correct the chainage number." << endl;
    cout << "D. calculate the distance." << endl;
    cout << "Please choose your demand: ";
    cin >> ch;

    switch (ch)
    {
    case 'A':
        cout << "The chainage number is: " << x.getkm() << endl;
        break;
    case 'B':
        cout << "The chainage number is: " << x.getm() << endl;
        break;
    case 'C':
    {
        double cor;
        cout << "Please enter the correct number: ";
        cin >> cor;

        double corl;
        corl = x.inckm(cor);
        while (corl < 0)
        {
            cout << "Too many subtraction,the result is out of range!" << endl;
            cout << "Please enter the correct number again: ";
            cin >> cor;
            corl = x.inckm(cor);
        }
        int kilometer;
        double meter;
        kilometer = (int)corl;
        meter = (corl - kilometer) * 1000;
        cout << "The corrected chainage number is: " << x.c << "K" << kilometer << "+" ;
        cout << fixed << setprecision(3) << meter << endl
        break;
    }
    case 'D':
        cout << "Please enter another chainage number: ";
        string n;
        string N;
        cin >> n;

        while (checkNum(n)||compare(M,n) || checkTh(n))
        {
            cout << "Please enter the correct number:";
            cin >> n;
        }N = n;
        Mileage y(N);
        double h = y.getkm();
        double dist;
        dist = getdist(g, h);
        cout << "The distance between them is: " << dist << "km" << endl;
        break;
    }

    system("pause");
    return 0;
}
  • 写回答

1条回答 默认 最新

  • Italink 2019-08-15 15:12
    关注

    开断点单步调试,可以找到在哪里出现问题,改一下就好;

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序