首页 > 其他 > 详细

1071

时间:2019-02-04 11:14:57      阅读:180      评论:0      收藏:0      [点我收藏+]
  • string的添加用+就可以了,insert是两个参数,一个pos(要用迭代器),一个是插入的元素
  • 之前的代码有一个bug最后一个单词不会读入,因为没有碰到其他字符   比如只有一个  A时,A不会读入
#include <cstdio>
#include <string>
#include <iostream>
#include <map>
using namespace std;
//被非字母数字分割开的所有


int main()
{
    map<string, int> words;
    string line;
    getline(cin,line);
    string word;
    for(string::iterator it=line.begin();it!=line.end();it++){
        if((*it>=a&&*it<=z)||(*it>=A&&*it<=Z)||(*it>=0&&*it<=9)){
           if(*it>=A&&*it<=Z){
                *it=a+(*it-A);
           }
           word.insert(word.end(),*it);
        }
        else if(!word.empty()){

            words[word]++;
            word.clear();
        }

    }
    if(!word.empty())words[word]++;
    int ma=0;
    string tmp;
    /*for(map<string,int>::iterator it=words.begin();it!=words.end();it++){
            tmp = it->first;
            cout<<tmp<<"  "<< it->second<<endl;

        }*/
    for(map<string,int>::iterator it=words.begin();it!=words.end();it++){
        if(it->second > ma){
            ma=it->second;

            tmp=it->first;

        }
    }
    cout<<tmp<<" "<<ma<<endl;





    return 0;
}
  • 可以像书上写得那样,如果碰到单词,则一次性读完,读完后直接存入MAP;而不用一个一个读,读到非法字符在存入;
  • 同理读到非法单词后,则一次性读完所有非凡的字符

1071

原文:https://www.cnblogs.com/flipped415/p/10351502.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!