• 输入若干行正文,输出其中含有给定单词的那些行


    是在贴吧看到的一个问题,代码如下,一如既往void foo (in{

        auto str = to_string (i);
        cout << "The length is: " << str.length () << endl;
        cout << str << endl;
        cout << string (str.crbegin (), str.crend ());
    }
    
    
    vector<vector<string>>
    matchLines (map<size_t, vector<string>> content, string keyWord)
    {
        vector<vector<string>> result;
        bool matchTheKeyWord = false;
        
        for (const auto &record : content) {
            for (const auto &word : record.second) {
                if (word == keyWord) {
                    matchTheKeyWord = true;
                    break;
                }
            }
            
            if (matchTheKeyWord) {
                result.push_back (record.second);
                matchTheKeyWord = false;
            }
        }
        
        return move (result);
    }
    
    void foo ()
    {
        size_t lineNo = 0;
        string line;
        map<size_t, vector<string>> inputContent;
        
        while (getline (cin, line)) {
            stringstream wordStream (line);
            string word;
            vector<string> words;
            
            while (wordStream >> word) {
                words.push_back (word);
            }
            
            ++lineNo;
            inputContent.insert (make_pair (lineNo, words));
        }
        
        cout << "Input your key word: ";
        string keyWord;
        cin.clear();
        clearerr(stdin);
        cin >> keyWord;
        auto result = matchLines (inputContent, keyWord);
        for (const auto & line : result) {
            for (const auto &word : line) {
                cout << word << " ";
            }
            cout << endl;
        }
    }
  • 相关阅读:
    教学计划-物理必修二
    小白学习Python之路---开发环境的搭建
    解决pycharm连接MySQL 1366报错的问题
    Leetcode 118 杨辉三角
    Leecode 70 爬楼梯
    RabbitMQ
    Leetcode 38 报数
    Leecode 69 x的平方根
    select 实现server I/O多路复用通信
    Leetcode 67 二进制求和
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4634672.html
Copyright © 2020-2023  润新知