Я обновляю ваш код в следующей точке: -
blockquote>#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; }
Да, разделение - то, что Вы хотите.
@tokens = split(/:/, "a:b:c:d");
foreach my $token (@tokens) {
....
}
Можно использовать разделение. Можно также использовать его с regex.
my @tokens = split(/:/,$string);
Для более усовершенствованного парсинга я рекомендую Синтаксический анализ:: RecDescent
если Вы имеете:
$a = "a:b:c:d";
@b = split /:/, $a;
затем Вы добираетесь:
@b = ("a", "b", "c", "d")
В целом это то, как разделенные работы:
split /PATTERN/,EXPR
Где PATTERN
может быть в значительной степени regex. Вы не ограничены простыми маркерами как':
'
Также смотрите на документацию, которая идет с жемчугом путем ввода при приглашении командной строки:
perldoc -f split
Искать использование часто задаваемых вопросов
perldoc -q split