首页 > 其他 > 详细

提取单词并表明行号

时间:2021-03-28 17:05:25      阅读:17      评论:0      收藏:0      [点我收藏+]

#include <bits/stdc++.h>
using namespace std;

typedef vector<string>::size_type line_no;
shared_ptr<vector<string>> file;
map<string, shared_ptr<set<line_no>>> wm;

shared_ptr<vector<string>> handlePunct(const string &s) {
    shared_ptr<vector<string>> p =
        make_shared<vector<string>>();
    size_t first = 0, idx = 0;
    while (idx != s.size()) {
        if (ispunct(s[idx])) {
            string word = s.substr(first, idx - first);
            if (!word.empty()) 
                p->push_back(word);
            p->push_back(s.substr(idx, 1));
            ++idx;
            first = idx;
        }
        else 
            ++idx;
    }
    string trail = s.substr(first);
    if (!trail.empty()) 
        p->push_back(trail);
    
    return p;
}

void func(ifstream &is) {
    file = make_shared<vector<string>> ();
    using std::getline;
    string text;
    while (getline(is, text)) {
        file->push_back(text);
        int n = file->size() - 1;
        istringstream line(text);
        string word;
        while (line >> word) {
            auto p = handlePunct(word);
            for (auto w : *p) {
                auto &lines = wm[w];
                if (!lines) 
                    lines.reset(new set<line_no>);
                lines->insert(n);
            }
        }
    }
}

int main() {
    ifstream in("in.txt");
    func(in); 
    for (auto t : wm) {
        string s = t.first;
        auto p = t.second;
        auto &tmp = *p;
        cout << s << ‘\n‘;
        for (auto k : tmp) cout << k << ‘ ‘;
        cout << ‘\n‘;
    }
    return 0;
}

提取单词并表明行号

原文:https://www.cnblogs.com/phr2000/p/14588892.html

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