我正在寻找可以解决此问题的C ++中的 特定设计模式。
。我想设计一个情节提要。我们的情节提要 包含任意许多笔记(想象一下,就像将便签放在板上)。 每个笔记都有一个标题,一个文本和一组标签。例如。 -标题:“ Test Traceplayer” -文本:“为spark核心框架的Traceplayer类执行单元测试。” -标签:{“单元测试”,“ traceplayer”,“测试”,“火花核心”}
我们的分镜脚本应该使我们能够按标题,文本和标签搜索注释。 例如。: searchByTitle(“测试Traceplayer”) searchByTag({“ testing”,“ unit test”}) searchByText(“为spark核心框架的Traceplayer类执行单元测试。”) 为了简单起见,我们不想在以下情况下进行任何相似性或前缀匹配 搜索标题,标签或文本。只有完全匹配才能给出结果。
我有许多解决此问题的解决方案O(1)搜索复杂性,但是任何人都可以提出解决该问题的任何“设计模式”。
解决三个STL映射问题并获得恒定的时间搜索复杂性
寻找可以解决此问题的特定设计模式。
我已经使用3个STL Map解决了这个问题,并且解决方案获得了O(1)搜索复杂性
#include <iostream>
#include <vector>
#include <map>
#define INPUT 8
class Note {
public:
string Tital;
string Text;
vector<string> vec;
Note(){
Tital = "\0";
Text = "\0";
}
};
class storyBoard{
public:
void AddNote(string Tital,string Text,vector<string> vec );
void RemoveByTital(string &tital);
void PrintStoredData();
Note* searchByTitle(string titleSearch);
Note* searchByText(string text_);
vector<Note*> searchByTag(string titleSearch);
void printSlip(Note *tm);
storyBoard(){}
private:
std::map<string,Note *> TitalMap;
std::map<string,Note *> TextMap;
std::map<string,std::vector<Note *> > TagsMap;
};
Note* storyBoard::searchByTitle(string titleSearch){
auto it_v = TitalMap.find(titleSearch);
if (it_v != TitalMap.end()){
cout<< "Tital search result is below:-"<<endl;
return it_v->second;
} else {
cout <<"data "<<titleSearch << " Not found"<<endl;
return NULL;
}
}
Note* storyBoard::searchByText(string titleSearch){
auto it_v = TextMap.find(titleSearch);
if (it_v != TextMap.end()){
cout<< "Text search result is below:-"<<endl;
return it_v->second;
} else {
cout <<"data "<<titleSearch << " Not found"<<endl;
return NULL;
}
}
vector<Note*> storyBoard::searchByTag(string tagSearch){
auto it_v = TagsMap.find(tagSearch);
if (it_v != TagsMap.end()){
cout<< "Tag search result is below:-"<<endl;
return it_v->second;
} else {
cout <<"data "<<tagSearch << " Not found"<<endl;
vector<Note*> del;
return del;
}
}
void storyBoard::AddNote(string Tital, string Text, vector<string> v){
Note *note = new Note;
note->Tital = Tital;
note->Text = Text;
note->vec = v;
TitalMap[note->Tital] = note;
TextMap[note->Text] = note;
for (auto it = note->vec.begin(); it != note->vec.end(); ++it){
//check that is tags already
auto it_v = TagsMap.find(*it);
if (it_v != TagsMap.end()){
it_v->second. push_back(note);
} else {
vector<Note *> &v = TagsMap[*it];
v.push_back(note);
}
}
}
void storyBoard::printSlip(Note *tm){
cout << "Tital=" << tm->Tital <<endl
<< "Text=" << tm->Text <<endl
<< "Tags = ";
for (auto it = tm->vec.begin(); it != tm->vec.end(); ++it){
cout<< *it<<"\t";
}
cout<<endl<<endl;
}
void storyBoard::PrintStoredData(){
for(auto tm : TitalMap){
printSlip(tm.second);
}
cout<<endl;
}
void feed_data_for_testing(storyBoard &Sb);
void TestCase(storyBoard &Sb);
int main() {
storyBoard Sb;
feed_data_for_testing(Sb);
Sb.PrintStoredData(); /*Print all contain data */
cout<<"************* From Here start searching ************"<<endl;
TestCase(Sb);
return 0;
}
void TestCase(storyBoard &Sb){
Note* obj = Sb.searchByTitle("Tital-3");
if(obj != NULL){
Sb.printSlip(obj);
}
obj = Sb.searchByText("Text-4");
if(obj != NULL){
Sb.printSlip(obj);
}
vector<Note *> vec = Sb.searchByTag("tag-3");
if(vec.size() !=0){
for (auto it = vec.begin(); it != vec.end(); ++it){
//cout<<(*it)->Tital << "\t";
Sb.printSlip(*it);
}
}
}
void feed_data_for_testing(storyBoard &Sb){
vector<string> tags ;
int count =INPUT;
for(int i =1;i<=count;i++){
string tital = "Tital-" + std::to_string(i);
string text = "Text-" + std::to_string(i);
tags.clear();
for(int j =1;j<=i;j++){
string tag_ = "tag-" + std::to_string(j);
tags.push_back(tag_);
}
Sb.AddNote(tital,text,tags);
}
}
我正在寻找一种解决该问题的设计模式。
答案 0 :(得分:0)
我在以下几点更新您的代码:-
#define INPUT 8 using namespace std; /*use class to store single slip data*/ class Note { public: string Tital; string Text; vector<string> vec; Note(){ //constructor to initialize data zero Tital = "\0"; Text = "\0"; } }; /*create a singalton pattern class so that create only one data storage*/ class storyBoard{ public: static storyBoard* getInstance(){ storyBoard* Instance= instance.load(); if ( !Instance ){ std::lock_guard<std::mutex> myLock(lock); Instance= instance.load(); if( !Instance ){ Instance= new storyBoard(); instance.store(Instance); } } return Instance; } void AddNote(string Tital,string Text,vector<string> vec ); void RemoveByTital(string &tital); void PrintStoredData(); Note* searchByTitle(string titleSearch); Note* searchByText(string text_); vector<Note*> searchByTag(string titleSearch); void printSlip(Note *tm); private: storyBoard()= default; ~storyBoard()= default; storyBoard(const storyBoard&)= delete; storyBoard& operator=(const storyBoard&)= delete; static std::atomic<storyBoard*> instance; static std::mutex lock; std::unordered_map<string,Note *> TitalMap; std::unordered_map<string,Note *> TextMap; std::unordered_map<string,std::vector<Note *> > TagsMap; }; std::atomic<storyBoard*> storyBoard::instance; std::mutex storyBoard::lock; Note* storyBoard::searchByTitle(string titleSearch){ auto it_v = TitalMap.find(titleSearch); if (it_v != TitalMap.end()){ cout<< "Tital search result is below:-"<<endl; return it_v->second; } else { cout <<"data "<<titleSearch << " Not found"<<endl; return NULL; } } Note* storyBoard::searchByText(string titleSearch){ auto it_v = TextMap.find(titleSearch); if (it_v != TextMap.end()){ cout<< "Text search result is below:-"<<endl; return it_v->second; } else { cout <<"data "<<titleSearch << " Not found"<<endl; return NULL; } } vector<Note*> storyBoard::searchByTag(string tagSearch){ auto it_v = TagsMap.find(tagSearch); if (it_v != TagsMap.end()){ cout<< "Tag search result is below:-"<<endl; return it_v->second; } else { cout <<"data "<<tagSearch << " Not found"<<endl; vector<Note*> del; return del; } } void storyBoard::AddNote(string Tital, string Text, vector<string> v){ Note *note = new Note; note->Tital = Tital; note->Text = Text; note->vec = v; TitalMap[note->Tital] = note; TextMap[note->Text] = note; for (auto it = note->vec.begin(); it != note->vec.end(); ++it){ //check that is tags already auto it_v = TagsMap.find(*it); if (it_v != TagsMap.end()){ it_v->second. push_back(note); } else { vector<Note *> &v = TagsMap[*it]; v.push_back(note); } } } void storyBoard::printSlip(Note *tm){ cout << "Tital=" << tm->Tital <<endl << "Text=" << tm->Text <<endl << "Tags = "; for (auto it = tm->vec.begin(); it != tm->vec.end(); ++it){ cout<< *it<<"\t"; } cout<<endl<<endl; } void storyBoard::PrintStoredData(){ for(auto tm : TitalMap){ printSlip(tm.second); } cout<<endl; } /**temporary function only use for testing*/ void TestCase(){ storyBoard *Sb = storyBoard::getInstance(); Note* obj = Sb->searchByTitle("Tital-3"); if(obj != NULL){ Sb->printSlip(obj); } obj = Sb->searchByText("Text-4"); if(obj != NULL){ Sb->printSlip(obj); } vector<Note *> vec = Sb->searchByTag("tag-3"); if(vec.size() !=0){ for (auto it = vec.begin(); it != vec.end(); ++it){ //cout<<(*it)->Tital << "\t"; Sb->printSlip(*it); } } } /**temporary function only use for testing*/ void feed_data_for_testing(){ storyBoard *Sb = storyBoard::getInstance(); vector<string> tags ; int count =INPUT; for(int i =1;i<=count;i++){ string tital = "Tital-" + std::to_string(i); string text = "Text-" + std::to_string(i); tags.clear(); for(int j =1;j<=i;j++){ string tag_ = "tag-" + std::to_string(j); tags.push_back(tag_); } Sb->AddNote(tital,text,tags); } } int main() { storyBoard *Sb = storyBoard::getInstance(); feed_data_for_testing(); Sb->PrintStoredData(); /*Print all contain data */ cout<<"************* From Here start searching ************"<<endl; TestCase(); return 0; }
答案 1 :(得分:0)
我认为您的面试官错误地使用了设计模式一词代替了惯用语。
您的代码的一个主要问题(可能是拒绝的原因)是使用经典C ++习惯用法的内存处理:
StoryBoard
对象的复制/分配(/移动)请注意,使用智能指针只是管理内存的一种方法。您也可以使用其他习语:
此问题解决后,您将遇到一些小问题:
StoryBoard
是内存的所有者,您不应返回调用者可能无意中释放的指针。如果我在解释面试官所说的内容时没有错,则应将此问题移至codereview.stackexhange.com