Hyunjinlele 2023-04-16 18:06 采纳率: 33.3%
浏览 19
已结题

C++程序运行时显示越界应如何解决

求问这个C++程序运行出现越界该如何解决?


#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;

struct S
{
    string t1,t2,station;    
};
class test
{
private:
    fstream f;
    string s,ss;
    string::size_type n,n1,n2,pos;
    map<string,S>a;
    map<string,S>::iterator p;
public:    
    test(int m)
    {
        if(m==1)
        {
            f.open("d:\\temp\\G79.txt",ios::in);
            if(!f)
            {
                cout<<"file error!"<<endl;
            }
            else 
            {
                S temp;
                a.clear();
                while(true)
                {
                    n=s.size();
                    getline(f,s);
                    if(!f.eof())
                    {
                        temp.t1=s.substr(0,5);
                        temp.t2=s.substr(n-5,5);
                        temp.station=s.substr(5,n-10);
                        a.insert(pair<string,S>(temp.station,temp));
                    }
                    else break;
                }
            }
            f.close();
        }
        else if(m==2)
        {
            f.open("G79.txt",ios::in|ios::binary);
            if(!f)
            {
                cout<<"file error!"<<endl;
            }
            else
            {
                f.seekg(0,ios::end);
                n=f.tellg();
                ss.resize(n);
                f.seekg(0,ios::beg);
                f.read((char*)ss.c_str(),ss.size());
                S temp;
                pos=0;
                while(true)
                {
                    if(ss.find("\n",pos)!=string::npos)
                    {
                        n1=pos;
                        n2=ss.find("\n",n1+1);
                        s=ss.substr(n1,n2-n1-1);
                        temp.t1=s.substr(0,5);
                        temp.t2=s.substr(n-5,5);
                        temp.station=s.substr(5,n-10);
                        a.insert(pair<string,S>(temp.station,temp));
                        pos=n2+1;
                        if(pos+1>=ss.size()) break;
                    }
                    else break;
                } 
            }
        }
    }
    void browse()
    {
        for(p=a.begin();p!=a.end();++p)
        {
            cout<<p->first<<"-"<<p->second.t1<<"-"<<p->second.t2<<endl;
        }
    }
    void Find()
    {
        string temp;
        cout<<"input station:";
        cin>>temp;
        p=a.find(temp);
        if(p!=a.end())
        {
            cout<<p->first<<"-"<<p->second.t1<<"-"<<p->second.t2<<endl;
        }
        else cout<<"no station!"<<endl;
    }
};
int main()
{
    test t(1);
    t.browse();
    t.Find();
    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 语言-逆行者 2023-04-16 19:47
    关注

    基于new BIng的修改:

    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    struct S
    {
    string t1,t2,station;
    };
    class test
    {
    private:
    fstream f;
    string s,ss;
    string::size_type n,n1,n2,pos;
    map<string,S>a;
    map<string,S>::iterator p;
    public:
    test(int m)
    {
    if(m==1)
    {
    f.open("d:\temp\G79.txt",ios::in);
    if(!f)
    {
    cout<<"file error!"<<endl;
    }
    else
    {
    S temp;
    a.clear();
    while(getline(f,s))
    {
    n=s.size();
    if(n >= 10) // 防止字符串长度小于10导致越界
    {
    temp.t1=s.substr(0,5);
    temp.t2=s.substr(n-5,5);
    temp.station=s.substr(5,n-10);
    a.insert(pair<string,S>(temp.station,temp));
    }
    }
    }
    f.close();
    }
    else if(m==2)
    {
    f.open("G79.txt",ios::in|ios::binary);
    if(!f)
    {
    cout<<"file error!"<<endl;
    }
    else
    {
    f.seekg(0,ios::end);
    n=f.tellg();
    ss.resize(n);
    f.seekg(0,ios::beg);
    f.read((char*)ss.c_str(),ss.size());
    S temp;
    pos=0;
    while(true)
    {
    if(ss.find("\n",pos)!=string::npos)
    {
    n1=pos;
    n2=ss.find("\n",n1+1);
    s=ss.substr(n1,n2-n1-1);
    n=s.size();
    if(n >= 10) // 防止字符串长度小于10导致越界
    {
    temp.t1=s.substr(0,5);
    temp.t2=s.substr(n-5,5);
    temp.station=s.substr(5,n-10);
    a.insert(pair<string,S>(temp.station,temp));
    }
    pos=n2+1;
    if(pos+1>=ss.size()) break;
    }
    else break;
    }
    }
    }
    }
    void browse()
    {
    for(p=a.begin();p!=a.end();++p)
    {
    cout<<p->first<<"-"<<p->second.t1<<"-"<<p->second.t2<<endl;
    }
    }
    void Find()
    {
    string temp;
    cout<<"input station:";
    cin>>temp;
    p=a.find(temp);
    if(p!=a.end())
    {
    cout<<p->first<<"-"<<p->second.t1<<"-"<<p->second.t2<<endl;
    }
    else cout<<"no station!"<<endl;
    }
    };
    int main()
    {
    test t(1);
    t.browse();
    t.Find();
    return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月29日
  • 已采纳回答 4月21日
  • 创建了问题 4月16日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)