Skip to content

Commit 5c7966a

Browse files
committed
delete Data Structure
2 parents b255553 + 32b4471 commit 5c7966a

File tree

7 files changed

+122
-1
lines changed

7 files changed

+122
-1
lines changed

TextQuery/TextQuery.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "TextQuery.h"
2+
3+
4+
TextQuery::TextQuery(ifstream& infile):file(new vector<string>)
5+
{
6+
string text;
7+
while(getline(infile, text)) //对文件中的每一行
8+
{
9+
file->push_back(text); //保存一行文本
10+
int n = file->size()-1; //记录行号
11+
istringstream line(text); //分配一个字符串流
12+
string word;
13+
while(line >> word) //提取一行字符串中的没一个单词
14+
{
15+
auto &lines = wm[word]; //lines 的类型为mapped_type是一个shared_ptr,指向set<line_no>
16+
if(!lines) //如果word在wm中不存在,则lines是一个空指针
17+
{
18+
lines.reset(new set<line_no>); //如果不存在,为lines分配一个set空间,并用reset使lines指向新分配的set
19+
}
20+
lines->insert(n); //将行号插入到对应word的set中
21+
}
22+
}
23+
}
24+
QueryResult TextQuery::query(const string& word) const
25+
{
26+
static shared_ptr<set<line_no>> nodata(new set<line_no>); //如果未找到word,就返回这个指针
27+
auto loc = wm.find(word);
28+
if(loc == wm.end())
29+
{
30+
return QueryResult(word, file, nodata); //未找到
31+
}
32+
else
33+
{
34+
return QueryResult(word, file, loc->second);
35+
}
36+
37+
38+
}
39+
40+
ostream& print(ostream &os, const QueryResult& qr)
41+
{
42+
os << qr.sought << " occurs " << qr.lines->size()
43+
<< "times:" << endl;
44+
for(auto it : *qr.lines)
45+
{
46+
cout << "\t (line " << it+1 << ") ";
47+
cout << *(qr.file->begin() + it) <<endl;
48+
}
49+
return os;
50+
}
51+
void runQueries(ifstream& infile)
52+
{
53+
//infile 是一个ifstream,指向我们要处理的文件
54+
TextQuery tq(infile); //保存文件并建立map
55+
//与用户交互:提示用户要输入的单词,完成查询并打印结果
56+
while(true)
57+
{
58+
cout << "enter word to look for or 'q' to quit: ";
59+
string s;
60+
if(!(cin >> s) || s == "q")
61+
break;
62+
else
63+
print(cout, tq.query(s)) << endl;
64+
}
65+
}
66+
int main()
67+
{
68+
69+
ifstream infile("TextQuery.txt");
70+
runQueries(infile);
71+
return 0;
72+
}

TextQuery/TextQuery.exe

213 KB
Binary file not shown.

TextQuery/TextQuery.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <map>
5+
#include <set>
6+
#include <fstream>
7+
#include <memory>
8+
#include <sstream>
9+
using namespace std;
10+
using line_no = vector<string>::size_type;
11+
class QueryResult;
12+
class TextQuery
13+
{
14+
public:
15+
16+
TextQuery(ifstream &infile);
17+
QueryResult query(const string &word) const;
18+
private:
19+
//定义输入文件的智能指针
20+
shared_ptr<vector<string>> file;
21+
//每个单词到它所在行号的集合的映射
22+
map<string, shared_ptr<set<line_no>>> wm;
23+
};
24+
class QueryResult
25+
{
26+
private:
27+
string sought; //查询的单词
28+
shared_ptr<vector<string>> file; //指向查询文件的指针
29+
shared_ptr<set<line_no>> lines; //指向行号的指针
30+
public:
31+
QueryResult(string s, shared_ptr<vector<string>> p, shared_ptr<set<line_no>> l):sought(s), file(p), lines(l){}
32+
friend ostream& print(ostream&, const QueryResult&);
33+
34+
35+
};

TextQuery/TextQuery.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
When love beckons to you follow him
2+
though his ways are
3+
hard and steep And when
4+
his wings enfold you
5+
yield to him though the sword
6+
hidden among his
7+
pinions may wound you
8+
And when he speaks to you
9+
believe in him
10+
though his voice
11+
may shatter your dreams as
12+
the north wind lays
13+
waste the garden.
14+

wilberWorkSpace/app.py

Whitespace-only changes.

xiangyu/simple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include<stdio.h>
1+
#include <stdio.h>
22
void main()
33
{
44
printf("Hello World!");

0 commit comments

Comments
 (0)