| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1256 人关注过本帖
标题:从linux下复制的一段输入,在windows下运行为什么会出问题?
只看楼主 加入收藏
青蝶
Rank: 2
等 级:论坛游民
帖 子:160
专家分:51
注 册:2018-2-4
结帖率:92%
收藏
已结贴  问题点数:20 回复次数:1 
从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
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:20 
你的 string 越界了
2018-10-21 18:05
快速回复:从linux下复制的一段输入,在windows下运行为什么会出问题?
数据加载中...
 
   



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

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