从linux下复制的一段输入,在windows下运行为什么会出问题?
这是我的程序:#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<functional>
using namespace std;
class Book{
public:
string name;
string author;
friend bool operator < (Book p,Book q){
if(p.author!=q.author){
if(p.author>q.author) return false;
else return true;
}
else{
if(p.name>q.name) return false;
else return true;
}
}
}book[1010];
int n=0;
map<string,int> value; // 0表示借出去了,1表示在书架,2表示借出去又还回来了但是还没放回书架。
int find(int now){
int i;
for(i=now-1;i>=0;i--){
if(value[book[i].name]==1) return i;
}
return -1;
}
int main(void){
char str[200];
string temp,t;
int pos,i;
value.clear();
while(getline(cin,temp)){
if(temp=="END") break;
book[n].name=temp.substr(0,temp.find_last_of("\"")+1);
book[n].author=temp.substr(temp.find_last_of("\""));
value[book[n].name]=1;
n++;
}
sort(book,book+n);
while(cin>>temp){
if(temp=="END") break;
if(temp=="BORROW"){
getchar();
getline(cin,t);
value[t]=0;
}
else if(temp=="RETURN"){
getchar();
getline(cin,t);
value[t]=2;
}
else{
for(i=0;i<n;i++){
if(value[book[i].name]==2){
pos=find(i);
if(pos==-1) cout<<"Put "<<book[i].name<<" first"<<endl;
else cout<<"Put "<<book[i].name<<" after "<<book[pos].name<<endl;
value[book[i].name]=1;
}
}
cout<<"END"<<endl;
}
}
return 0;
}
从linux下复制的一段输入:
"The Canterbury Tales" by Chaucer, G. "Algorithms" by Sedgewick, R. "The C Programming Language" by Kernighan, B. and Ritchie, D. END BORROW "Algorithms" BORROW "The C Programming Language" RETURN "Algorithms" RETURN "The C Programming Language" SHELVE END
输出是这样的:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 18446744073709551615) > this->size() (which is 0)
我知道linux和windows下的换行符不一样,如果把输入在windows里换行,变成这样:
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
就可以输出正确结果了:
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END
求大佬帮忙看看为什么