// 文本查詢程序-My.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<memory>
#include<initializer_list>
using namespace std;
class StrVec
{
using size_type = vector<string>::size_type;
public:
StrVec():data(new vector<string>()){} //默認(rèn)初始化!
StrVec(initializer_list<string> il):data(make_shared<vector<string>>(il)) {} //il就這么用!
//StrVec(const StrVec &rh):data(copy_shared(rh)){}智能指針與拷貝構(gòu)造函數(shù)、拷貝賦值、移動(dòng)構(gòu)造函數(shù)、移動(dòng)賦值的使用樣本?》
~StrVec()=default;
void push_back(const string &s);
void pop_back();
size_type size()const { return data->size(); }
bool empty()const { return data->empty(); }
string &front();
string &back();
string& operator[] (const size_type &i);
string& operator[] (const size_type &i) const;
explicit operator bool()const { return data->size() > 0 ? true : false; }
private:
shared_ptr<vector<string>> data;
void check(unsigned i) //檢查data[i]是否越界?
{
if (i >= data->size())
throw out_of_range("Out_of_range!");
}
shared_ptr<vector<string>> copy_shared(const StrVec &rh)
{
shared_ptr<vector<string>> ret;
auto beg = ret->begin();
uninitialized_copy(rh.data->cbegin(), rh.data->cend(), beg++);
return ret;
}
};
void StrVec::push_back(const string &s)
{
data->push_back(s);
}
void StrVec::pop_back()
{
check(0);
data->pop_back();
}
string& StrVec::front()
{
check(0);
return data->at(0);
}
string& StrVec::back()
{
check(0);
return data->at(data->size()-1);
}
string& StrVec::operator[](const size_type &i)
{
check(i);
return data->at(i);
}
string& StrVec::operator[] (const size_type &i) const //區(qū)分StrVec本身,const版本和非const版本!
{
//check(i); //check暫時(shí)沒有定義const版本,故在此舍棄!
return data->at(i);
}
class QueryR
{
public:
QueryR(const string &s, shared_ptr<map<string, set<unsigned>>> l, StrVec f) :word(s), linenu(l), file(f) {}
~QueryR() = default;
friend ostream& operator<<(ostream &os, QueryR rh);
private:
string word;
shared_ptr<map<string, set<unsigned>>> linenu;
StrVec file;
};
ostream& operator<<(ostream &os, QueryR rh)
{
os << rh.word <<" occurs " << rh.linenu->at(rh.word).size() << " times" << endl;
for (auto &r : rh.linenu->at(rh.word))
{
os << "\t(line " << r+1 << ")" << rh.file[r]<<endl;// const QueryR,其file不能使用[]???
}
return os;
}
class Text
{
public:
Text(const string &filename):linenu(make_shared<map<string,set<unsigned>>>()) { read(filename); }
~Text()=default;
QueryR query(const string &s);
private:
shared_ptr<map<string, set<unsigned>>> linenu; //要么在構(gòu)造函數(shù)中綁定,要么在函數(shù)中綁定內(nèi)存!
StrVec file; //由類自己決定
void read(const string &filename);
};
void Text::read(const string &filename)
{
ifstream f(filename);
if (f)
{
string tmp;
unsigned row = 0; //單詞所在的行號(hào)
while (getline(f,tmp))
{
istringstream line(tmp);
string word;
while (line>>word)
{
if(linenu->find(word)==linenu->cend())
(*linenu)[word]; //如果不存在,則創(chuàng)建pair<string,set<unsigned>>
(*linenu)[word].insert(row); //單詞word,插入行號(hào)row
}
file.push_back(tmp); //保存每一行的tmp
row = row + 1;
}
}
f.close();
}
QueryR Text::query(const string &s)
{
if (linenu->find(s) != linenu->cend())
return QueryR(s, linenu, file);
else
return QueryR(s, nullptr, file);
}
int main()
{
string filename = "C:\\Users\\winack\\Documents\\Visual Studio 2017\\Projects\\文本查詢程序-My\\123.txt";
Text info(filename);
cout << info.query("is");
StrVec sv;
sv.push_back("asdfg");
const StrVec &ssv = sv;
cout <<"非const版本: "<<sv[0]<<" && "<<"是const版本: "<< ssv[0] << endl;
return 0;
}
文本查詢-My(StrVec提供類間共享元素的能力?。?/h2> 最后編輯于 :?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 七百只龍蝦說(shuō) 因?yàn)橛X得自己沒有對(duì)專車和出租車的需求,所以錯(cuò)過了去年最激烈的打車大戰(zhàn),對(duì)Uber的認(rèn)知也是從那個(gè)勞什...
- 地下行駛半小時(shí) 13歲的女兒帶我去一個(gè)懷舊咖啡廳 我說(shuō),家門口的星巴克不是很好嗎? 女兒說(shuō),這是靠近藝術(shù)家的咖啡廳...
- 一部熱播劇《我的前半生》,再次引發(fā)了大眾頻繁刷屏。有人吐槽劇情陷入瑪麗蘇套路,女主自帶光環(huán),吸引閨蜜男友和前夫一路...
- 我認(rèn)識(shí)轉(zhuǎn)轉(zhuǎn)那一年是2001年,那一年她21歲,剛從農(nóng)村來(lái)到西安才一年。 那一年我剛大學(xué)畢業(yè)在做編輯,她老板開了個(gè)復(fù)...