dictionary - I am attempting a hash map that stores vector of objects. in C++ -
i attempting hash map stores vector of objects.
explanation / goal in words. if no key not found, create new entry. if key found, push new object vector of objects
in pseudo code attempting
map<string,vector<object>> data object{ int data vector<keywords> keywords } function(object arg) { local_key_var = object->keywords; ( key_element : local_key_var) { if(key_element not exist in vector<object> ) { create entry map<key_element,vector<object> } else { local_vector_var = current vectorobject; local_vetor_var.push(arg); erase current vector object; insert new_vector_var int map<key_element,vector<objects>>; } } }
in c++ code at.
void library::additembykeyword(item* item) { vector<string> localkeyvector = item->getkeywords(); (auto element : localkeyvector) // access reference avoid copying { map<string, vector<item**>>::const_iterator = keywordlib.find(element); if(it == keywordlib.end()) // not found { vector<item**> localvector; localvector.push_back(&item); keywordlib.insert(pair<string,vector<item**>>(item->gettitle(),localvector)); } else // found { // copy found stack of items vector vector<item**> localvector = it->second; // push passed in item on top of new copy localvector.push_back(&item); // remove old map entry // keywordlib.erase(it); // push new copy of vectors on top of hash // keywordlib.insert(pair<string,vector<item**>>(item->gettitle(),localvector)); } }
please note below code may not best attempt yet. have been stuck on longer care admit , think time someone.
Comments
Post a Comment