标题:从linux下复制的一段输入,在windows下运行为什么会出问题?
取消只看楼主
青蝶
Rank: 2
等 级:论坛游民
帖 子:160
专家分:51
注 册:2018-2-4
结帖率:92%
已结贴  问题点数:20 回复次数:0 
从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

求大佬帮忙看看为什么
搜索更多相关主题的帖子: include name return temp The 
2018-10-21 17:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-490567-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013489 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved