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


    是在贴吧看到的一个问题,代码如下,一如既往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;
        }
    }
  • 相关阅读:
    mysql 锁相关的视图(未整理)
    MYSQL的用户变量(@)和系统变量(@@)
    mysql杀死线程
    mysql 系统变量和session变量
    Oracle DB 移动数据
    MySQL数据库------常用函数
    练习-----查询
    练习建表
    课堂笔记总结
    数据库--------用代码来创建表
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4634672.html
Copyright © 2020-2023  润新知