高分求C++高手解题,关于容器的问题
1.单词出现的次数
2.列出每次出现所在的行和行号如果在一行出现多次,程序将显示该行一次。行号按升序显示。
例如,查找单词element。输出的前几行为:
Element occurs 125 times
(line 62) element with a given key.
(line 64) second element with the same key.
(line 153) element |==| operator.
需要定义各种容器来解决 展开
#include <iostream>
using namespace std;
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include "openFile.h"
#include <sstream>
//#include "quer.h"
class TextQuery
{
public:
typedef vector<string>::size_type line_no;
void read_file(ifstream &is)
{
store_file(is);
build_map();
};
set <line_no> run_query(const string &) const;
string text_line(line_no) const;
private:
void store_file(ifstream &);
void build_map();
vector<string> lines_of_text;
map<string,set<line_no> >word_map;
};
void TextQuery::store_file(ifstream &is)
{
string textline;
while (getline(is,textline))
{
lines_of_text.push_back(textline);
};
};
void TextQuery::build_map()
{
for(line_no line_num=0;line_num!=lines_of_text.size();++line_num)
{
istringstream line(lines_of_text[line_num]);
string word;
while(line>>word)
word_map[word].insert(line_num);
};
};
set<TextQuery::line_no> TextQuery::run_query(const string &query_word)const
{
map<string,set<line_no> >::const_iterator loc=word_map.find(query_word);
if(loc==word_map.end())
return set<line_no>();
else
return loc->second;
};
string TextQuery::text_line(line_no line) const
{
if(line< lines_of_text.size())
return lines_of_text[line];
throw out_of_range("line number out of range");
};
void print_results(const set<TextQuery::line_no> &locs,
const string &sought,const TextQuery &file)
{
typedef set<TextQuery::line_no> line_nums;
line_nums::size_type size=locs.size();
cout<<"\n"<<sought<<" occurs "<<size<<" "<<"times"<<endl;
line_nums::const_iterator it=locs.begin();
for(;it!=locs.end();++it)
{
cout<<"\t(line"<<(*it)+1<<")"<<file.text_line(*it)<<endl;
};
};
int main()
{
ifstream infile;
open_file(infile,"1.txt");
TextQuery tq;
tq.read_file(infile);
while(true)
{
cout<<"enter word to look for ,or q to quit : ";
string s;
cin>>s;
if(!cin||s=="q")
break;
set<TextQuery::line_no> locs=tq.run_query(s);
print_results(locs,s,tq);
};
return 0;
};
1.txt:的内容是
ss sdffs
sdf sdf sdf sgfs
fs ss sdf
sdfsg srgsg sdf ss
结果如图
楼主需要的功能很简单,没必要写那么麻烦,但是我没看懂你最后一个输出是什么意思,但是满足了你这两个条件:
1.单词出现的次数
2.列出每次出现所在的行和行号如果在一行出现多次,程序将显示该行一次。行号按升序显示。
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
fstream fin("file.txt", ios::in);
if(!fin)
{
cout << "cannot open file!" << endl;
return -1;
}
string temp, s, lag;
char str[256];
vector<string> a;
vector< vector<string> > res;
string::iterator it;
while(!fin.eof())
{
fin.getline(str,255);
s = str;
it = s.begin();
while(it != s.end())
{
for(; *it != ' ' && it != s.end(); it++)
{
lag = *it;
temp.append(lag);
}
if(it != s.end())
it++;
a.push_back(temp);
temp.erase(temp.begin(),temp.end());
lag.erase(lag.begin(),lag.end());
}
res.push_back(a);
a.erase(a.begin(),a.end());
}
fin.close();
cout << "input the string you want to find: ";
cin >> temp;
int sum = 0;
vector< vector<string> >::size_type templine;
for(vector< vector<string> >::size_type i = 0; i != res.size(); i++)
{
templine = i;
for(vector<string>::size_type j = 0; j < res[i].size(); j++)
{
if(res[i][j] == temp)
{
if(templine == i)
{
if(sum == 0)
cout << "( line " << i+1 << ") " << temp << " with a given key" << endl;
else
cout << "( line " << i+1 << ") " << temp << " with the same key" << endl;
templine = 0;
}
sum++;
}
}
}
cout << temp << " occurs " << sum << " times" << endl;
return 0;
}