您的位置:首页 > 编程语言 > C语言/C++

C++ primer testQuery

2014-06-01 16:21 162 查看
</pre><p><pre name="code" class="cpp">#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<map>
#include<set>
#include<memory>
using namespace std;

int main()
{
ifstream infile("data.txt");

map<string,shared_ptr<set<size_t>>> wm;
shared_ptr<vector<string>>   file(new vector<string>);

string line;
while(getline(infile,line))
{
file->push_back(line);
int n=file->size()-1;

istringstream line_word(line);
string  word;
while(line_word>>word)
{
auto &set_line=wm[word];
if(!set_line)
set_line.reset(new set<size_t>);
set_line->insert(n);
}
}

string sought;
shared_ptr<set<size_t>> nodata(new set<size_t>);

while(1)
{
cout<<"enter a word: ";
cin>>sought;
if(sought=="q") break;

auto loc_set=wm.find(sought);
if(loc_set!=wm.end())
{	cout<<sought<<" "<<(loc_set->second)->size()<<" time "<<endl;
for (auto num:(*(loc_set->second)))
cout<<"line "<<num+1<<" "<<*(file->begin()+num)<<endl;
}
else
{
cout<<"no data"<<endl;
}

}

return 0;
}


改了书上的代码。没有用到类。只是为了熟悉文件输入输出操作以及容器的使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: