[2018年5月4号]C++ primer 课后练习 第十二章 动态内存

12.26

    allocator<char> charAllocator;
    char * a = charAllocator.allocate(tmp.size()); 

仅仅将内存申请的方法改为如上即可

12.27

class TextQuery {
public:
    TextQuery() = default;
    TextQuery(ifstream& );

private:
    vector<string > vec;
    map<string, set<int>> wordMap;
};

TextQuery::TextQuery(ifstream& ifs){
    string lineStr, word;
    int lineCount = 0;
    while (getline(ifs, lineStr))
    {
        vec.push_back(lineStr);
        istringstream ist(lineStr);
        while (ist >> word && ist.eof())
        {
            wordMap[word].insert(lineCount);
        }
        lineCount++;
    }
    
}
12.28
int main(int argc, char *argv[]) {
    ifstream ifs(argv[1]);
    vector<string > vec;
    map<string, set<int>> wordMap;
    string lineStr, word;
    int lineCount = 0;
    while (getline(ifs, lineStr))
    {
        vec.push_back(lineStr);
        istringstream ist(lineStr);
        while (ist >> word && !ist.eof())
        {
            wordMap[word].insert(lineCount);
        }
        lineCount++;
    }
    string key = argv[2];

    set<int > lineSet = wordMap[key];
    if(lineSet.size()){
        for(set<int >::iterator it = lineSet.begin(); it != lineSet.end(); it++){
            cout << "(line: " << *it << " ) " << vec.at(*it) << endl;
        }
    }else{
        cout << "not have any key word in file" << endl;
    }

    for (;;);
    return 0;
}

12.29

倾向使用while版本,先判断再执行

12.30

class QueryResult;
using line_no = vector<string >::size_type;

class TextQuery {
public:
    TextQuery(ifstream&);
    QueryResult query(const string &)const;
private:
    shared_ptr<vector<string >> file;
    map<string, shared_ptr<set<line_no>>> wm;
};

TextQuery::TextQuery(ifstream & ifs):file(new vector<string>()){
    string lineStr, word;
    while (getline(ifs, lineStr))
    {
        file->push_back(lineStr);
        int lineCount = file->size();
        istringstream ist(lineStr);
        while (ist >> word)
        {
            shared_ptr<set<line_no>>& line = wm[word];
            if(!line){
                line.reset(new set<line_no>);
            }
            line->insert(lineCount);
        }
    }
}

class QueryResult {
friend ostream& print(ostream&, const QueryResult& );
public:
    QueryResult(string s, shared_ptr<set<line_no>> ss, shared_ptr<vector<string>> sp):s(s),ss(ss),sp(sp){};
private:
    string s;
    shared_ptr<set<line_no>> ss;
    shared_ptr<vector<string>> sp;
};

QueryResult TextQuery::query(const string & word)const{
    static shared_ptr<set<line_no>> noData(new set<line_no>);
    auto result = wm.find(word);
    if(result == wm.end()){
        return QueryResult(word, noData, file);
    }else{
        return QueryResult(word, wm.find(word)->second, file);
    }
}

ostream& print(ostream& os, const QueryResult & ptr){
    cout << "you find " << ptr.ss->size() << " in file" << endl;
    //for (set<line_no>::iterator it = *ptr.ss->begin(); it != *ptr.ss->end(); it++) {
    //    
    //}
    for(auto it : *ptr.ss){
        cout << "(line: " << it << " ) " << ptr.sp->at(it) << endl;
    }
    return os;
}

int main(int argc, char *argv[]) {
    ifstream ifs(argv[1]);
    TextQuery tq(ifs);
    print(cout,tq.query("happy"));
    for (;;);
    return 0;
}

12.31

set好,vector每次插入都要遍历容器是否已经存在当前元素

12.32


猜你喜欢

转载自blog.csdn.net/qq_22478401/article/details/80189592