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

是在贴吧看到的一个问题,代码如下,一如既往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;
    }
}
原文地址:https://www.cnblogs.com/wuOverflow/p/4634672.html