Categorizing and linking data

Hello!

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.

Any ideashow to do any of this is welcome!

You might want to use an associative container such as a std::map for this.
http://www.cplusplus.com/reference/map/map/

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").
Can i allocate memory to maps dynamically? Like "new map"?
How does multimaps work? for if i want to store more than one value to the key?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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
Yes, you can allocate maps dynamically, although it's a good idea to avoid doing so.
 
  map<string,string> * pmap = new map<string,string>; 


Multimaps remove the restriction that the key be unique.
1
2
  mmap.insert (pair<string,string>("tree","wood"));
  mmap.insert (pair<string,string>("tree","petrified"));



Thanks for the answers, i'm learning!

@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?
I'm not sure I understand your question.

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.
OK. This was very helpful. Im going to experiment with this for a bit. Looking forward to learning more advanced c++. Thank you guys!
Topic archived. No new replies allowed.