I'm trying to figure out how and what to use to categorize information. I also want to link the information so the program knows where the linked information is stored, not only during runtime but also saved data for example where in a text file.
I was thinking about using linked lists, but i don't know how to categorize information in a linked list other than just putting chunks of inforation apart from each other.
I have no code at this moment but i was thinking like this:
data1, data1.1, data1.2, BLANK, data2, data2.2 (AND SO ON)
and then another linked list where their linked data is stored, like if i want an object called "tree" to be linked to another object called "wood" i would link these.
The key_type would contain what uniquely identifies the entry ("tree").
The mapped_type can be a struct or class that holds the attributes of the entry ("wood").
#include <map>
struct key // the items in the map are referenced my this key
{
// anything can go here
};
struct data // this is the data referenced by the key
{
// anything can go here
};
int main ( )
{
std::map<key,data> newMap; // creates a new map. it doesn't have to be structs, any data type is valid
key a;
data b;
newMap.insert ( std::pair<key,data> ( a, b ) ); // insert item 'b' with key value 'a'
}
"STL map: Associative key-value pair held in balanced binary tree structure. Each key is unique.
STL multimap: Same as an STL map except that duplicate keys are allowed. " - http://www.yolinux.com/TUTORIALS/CppStlMultiMap.html
@AbstractionAnon: Are there any other way for me to let the user add new "trees" without letting the program know it should allocate when the user needs it?
Keep in mind that with maps and multimaps, only the "anchor" exists when you instantiate the map or multimap. There are no actual entries until you do an insert as I showed in the second snippet.
If you want to add a new kind of tree, you simply execute one of the lines I showed in the second snippet. The map/multimap takes care all the management. You don't need to worry about allocating anything. Both strings were inserted by value so cleanup is automatic. This is much easier than trying to allocate objects yourself and then trying to clean up the map entries when the map anchor goes out of scope.
In my previous example, I used a string for both the key_type and the mapped_type for simplicity. You are not restricted using strings. You can use any types you want, including structs and classes.
It's a good idea to keep the key_type to a type that has a bult-in < operator. If you use your own class for key_type, then you must overload the < operator so that map/multimap can determine where to insert the entry since maps/multimaps are inherently ordered.