zoukankan      html  css  js  c++  java
  • scanf函数读入整数后接着读字符串的换行符残余问题

    #include<iostream>
    #include<vector>
    #include<map>
    #include<sstream>
    #include<algorithm>
    using namespace std;
    map<string ,vector<string> >title,keywords,author,publisher,publisher_year;
    int n;
    bool cmp(string a,string b){
        return a<b;
    }
    void res(map<string ,vector<string> > &m,string &str){
        if(m[str].size() == 0)cout<<"Not Found
    ";
        else{
            sort(m[str].begin(),m[str].end(),cmp);
            for(auto i:m[str]){
                printf("%s
    ",i.c_str());
            }
        }
    }
    int main()
    {
        cin>>n;
    //    getchar();
        for(int i=1;i<=n;i++){
            string id;
            getline(cin,id);
            string s;
            getline(cin,s);
            title[s].push_back(id);
        //    getchar();
            getline(cin,s);
            author[s].push_back(id);
        //    getchar();
            while(cin>>s){
                keywords[s].push_back(id);
                char c = getchar();
                if(c == '
    ')break;
            } 
        //    getchar();
            getline(cin,s);
            publisher[s].push_back(id);
        //    getchar();
            getline(cin,s);
            publisher_year[s].push_back(id);
    //        getchar(); 
        }
        int m;
        cin>>m;
        string s;
        int tmp ; 
        for(int i=1;i<=m;i++){
            scanf("%d: ",&tmp);
            getline(cin,s);
            printf("%d: %s
    ",tmp,s.c_str()); 
            if(tmp == 1)res(title,s) ;
            else if(tmp == 2) res(author ,s);
            else if(tmp == 3) res(keywords,s);
            else if(tmp == 4) res(publisher,s);
            else if(tmp == 5) res(publisher_year,s);
        }
        return 0;
     } 

     scanf(),cin在读入一个数据时会把末尾的回车,换行留在缓冲区,如果后面紧接着读入字符串,需用space std;

    map<string ,vector<string> >title,keywords,author,publisher,publisher_year;

    int n;

    bool cmp(string a,string b){

        return a<b;

    }

    void res(map<string ,vector<string> > &m,string &str){

        if(m[str].size() == 0)cout<<"Not Found ";

        else{

            sort(m[str].begin(),m[str].end(),cmp);

            for(auto i:m[str]){

                printf("%s ",i.c_str());

            }

        }

    }

    int main()

    {

        cin>>n;

    // getchar();

        for(int i=1;i<=n;i++){

            string id;

            getline(cin,id);

            string s;

            getline(cin,s);

            title[s].push_back(id);

        // getchar();

            getline(cin,s);

            author[s].push_back(id);

        // getchar();

            while(cin>>s){

                keywords[s].push_back(id);

                char c = getchar();

                if(c == ' ')break;

            } 

        // getchar();

            getline(cin,s);

            publisher[s].push_back(id);

        // getchar();

            getline(cin,s);

            publisher_year[s].push_back(id);

    // getchar(); 

        }

        int m;

        cin>>m;

        string s;

        int tmp ; 

        for(int i=1;i<=m;i++){

            scanf("%d: ",&tmp);

            getline(cin,s);

            printf("%d: %s ",tmp,s.c_str()); 

            if(tmp == 1)res(title,s) ;

            else if(tmp == 2) res(author ,s);

            else if(tmp == 3) res(keywords,s);

            else if(tmp == 4) res(publisher,s);

            else if(tmp == 5) res(publisher_year,s);

        }

        return 0;

     } 

     scanf(),cin在读入一个数据时会把末尾的回车,换行留在缓冲区,如果后面紧接着读入字符串,需用先用getchar()函数吞掉那个回车符,再用getline函数多次读入字符串,getline()会将末尾换行符丢弃

  • 相关阅读:
    HDU 2546:饭卡(01背包)
    HPU 第三次积分赛:阶乘之和(水题)
    拓扑排序练习题
    HDU 2647:Reward(拓扑排序+队列)
    HDU 3342:Legal or Not(拓扑排序)
    HDU 2094:产生冠军(拓扑排序)
    POJ 2585:Window Pains(拓扑排序)
    51Nod 1002:数塔取数问题(DP)
    cogs696 longest prefix
    poj3764 The xor-longest Path
  • 原文地址:https://www.cnblogs.com/zxzmnh/p/11992017.html
Copyright © 2011-2022 走看看