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

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


#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct S
{
    string t1,t2,station;    
};
class test
{
private:
    fstream f;
    string s,ss;
    string::size_type n,n1,n2,pos;
    mapa;
    map::iterator p;
public:    
    test(int m)
    {
        if(m==1)
        {
            f.open("d:\\temp\\G79.txt",ios::in);
            if(!f)
            {
                cout<<"file error!"<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(temp.station,temp));
                    }
                    else break;
                }
            }
            f.close();
        }
        else if(m==2)
        {
            f.open("G79.txt",ios::in|ios::binary);
            if(!f)
            {
                cout<<"file error!"<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(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<first<<"-"<second.t1<<"-"<second.t2<Find()
    {
        string temp;
        cout<<"input station:";
        cin>>temp;
        p=a.find(temp);
        if(p!=a.end())
        {
            cout<first<<"-"<second.t1<<"-"<second.t2<else cout<<"no station!"<Find();
    return 0;
}

img

基于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;
}