c++结束了动态和关联容器的学习,其实动态内存我还没有弄会,这个需要多做题,多体会;
下面开始数据结构与算法的学习;务必效率和抓经时间;把该学的课程都不紧不慢的提上日程;加油!!!
/*--------------- 关键字查询的类 --------------*/
#ifndef _TEXT_QUERY_
#define _TEXT_QUERY_
#include<iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <memory>
#include <map>
#include <set>
using namespace std;
class TextResult;
class TextQuery;
class TextQuery
{
public:
TextQuery()=default;
TextQuery(fstream &file):line_txt(make_shared<vector<string>>()), _key(make_shared<map<string, set<int>>>())
{
string line;
int line_i=0;
while (getline(file, line))
{
++line_i;
line_txt->push_back(line);
istringstream iss(line);
string m_key;
while (iss >> m_key)
{
//这里有问题,如果一个单词在一行中出现的次数大于1,则计数就不对了
(*_key)[m_key].insert(line_i);
}
}
};
void query(const string _word);
~TextQuery() {};
private:
shared_ptr<vector<string>> line_txt;
shared_ptr<map<string, set<int>>> _key;
};
class TextResult
{
public:
TextResult() = default;
TextResult(const string &w, shared_ptr<vector<string>> &l, shared_ptr<map<string, set<int>>>& k) :
m_word(w), _line_txt(l), __key(k) {};
ostream &print(ostream &out)const;
private:
const string m_word;
shared_ptr<vector<string>> _line_txt;
shared_ptr<map<string, set<int>>> __key;
};
ostream &TextResult::print(ostream &out) const
{
if (__key->find(m_word) == __key->end())
{
throw runtime_error("no this word");
}
else {
out << m_word << " occurs " << __key->find(m_word)->second.size() << ((__key->find(m_word)->second.size() > 1) ? " times " : " time ")
<< endl;
for (auto &c : __key->find(m_word)->second)
out << "(line " << c << ") " << _line_txt->at(c-1) << endl;
return out;
}
}
void TextQuery::query(const string _word)
{
TextResult kill(_word, line_txt, _key);
kill.print(cout);
}
#endif // _TEXT_QUERY_
#include<iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <memory>
#include <map>
#include <set>
#include "TextQuery.h"
using namespace std;
void _make_rule(fstream &input, set<string> &_key, map<string, vector<string>> &_map);
void transform_word(fstream &_file_Rule, fstream &_file_Make);
void main(int argc,char* argv[])
{
/*--------------- 关于文件查询功能的动态内存的一个例子 -------------------------------*/
fstream iof("C:\\Users\\信计捡球员\\Desktop\\transform_word\\kill.txt");
TextQuery openfile(iof);
string word;
cin >> word;
openfile.query(word);
/*--------------------- 关于关联容器替换单词的程序 ----------------------*/
fstream iss(argv[1], fstream::in);
fstream inn(argv[2], ios::in );
transform_word(iss,inn);
iss.close();
inn.close();
system("pause");
}
void _make_rule(fstream &input,set<string> &_key,map<string,vector<string>> &_map)
{
string line;
string _my_key;
while (getline(input, line))
{
string phrase;
vector<string> _phrase;
istringstream _get_s(line);
_get_s >> _my_key;
_key.insert(_my_key); //对于set插入关键字
while (_get_s >> phrase)
{
_phrase.push_back(phrase);
}
_map.insert(pair<string, vector<string>>(_my_key, _phrase)); //初始化map
}
}
void transform_word(fstream &_file_Rule, fstream &_file_Make)
{
set<string> m_key;
map<string, vector<string>> m_map;
_make_rule(_file_Rule, m_key, m_map);
string _line;
while (getline(_file_Make, _line))
{
vector<string> _phrase;
istringstream words(_line);
string _W;
while (words >> _W)
{
_phrase.push_back(_W);
}
for (auto &__word :_phrase)
{
if (m_key.find(__word) != m_key.end()) //找到了此元素
{
if (m_map[__word].size() < 1)
{
throw runtime_error("no rule for " + __word);
}
string replace;
//*replace = "";
for (auto &c : m_map[__word]) //因为m_map的第二个元素是vector,无法直接将需要换的那一个string类型的单词替换
{
replace += c;
}
__word = replace;
}
}
for (auto &c : _phrase)
cout << c << " ";
cout << endl;
}
}