March 24th, 2004, 04:45 AM
what stl data structure to use
I'm having diffculties to find the right data structure for me
I have a structure with 5 fields
I need some kind of data structure that will hold unknown numbers of elements of myStruct
and I need to have each fields as a key field
If I have only 1 field as the key
I would have used hash_map
for example if the key is iField1
then I would have used hash_map with int as key and with myStruct as the data
I need to be able to have 5 keys (and return the correct element according to 1 of them, meaning the key is not combination of them, but each one of them should be able to retrive the correct element)
March 24th, 2004, 05:47 AM
Use vector<const myStruct> to store the data.
Use map<int,const myStruct&> to store an index that points to the correct value of myStruct for each value of iField1.
Use another map for each additional index.
Adding and deleting myStructs will be some work. Updateing even more, so I recommend implementing it as delete+insert (thus the const).
March 24th, 2004, 07:28 AM
March 24th, 2004, 01:14 PM
And, if you can, wrap the vector and maps up in another class to make the whole thing easier to use. E.g.
void add(const myStruct& s)
// push_back to the vector, update the maps, etc.
// other functions for getting at the data
std::map<int, myStruct*> map1_;
// etc for the other maps
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
March 30th, 2004, 12:09 PM
why would this be better than building a linked list with pointers to the next index by each field ie:
int filed1index(int i);
LInkList * myfield1next;
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!