Containers and iterators (Tutorail search)

Hi guys,

I've seen a new chapter in my schoolbook but it's 5 papers about Containers and iterators without any examples.

Does someone knows a good tutorial with an example? Because google only gives me something about STL and vector but that's not what i'm looking for.

You can compare it with C the structs with next pointer previous pointer...

kind regards,
Containers are any class that contains a multitude of similar data types (simple types like int, char, etc. but also advanced types like String and any class/struct you made).

The simplest container is an array: fixed size, linear storage unit of data.
A vector is a dynamically-sized array: it functions exactly like an array, but can increase and decrease its size [at a performance cost].
A linked list is an item class with a *next pointer. It can be "doubly linked" by including a *previous pointer as well. It is organized internally (i.e. the items take care of the order, in contrast to a vector that keeps items in place).

There are others (sets and maps, for instance, for associative data), but I've never really used them, so I can't really help you with those.
Oké but how do I link the iterator class to the container class? Because the container does have for example:

class ArtikelContainer
{
public:
Artikel *next;
private:
Artikel test1;
};

Ofc I need a constructor to ini the test1 but then the iterator class has functions like next and previous and stuff but how do I link them?

I don't find a good example on internet :(

Thanks for your help already.
An iterator just keeps a pointer to the current item and provides an easy interface for traversing a container.

In this case, you could say:
1
2
3
ArtikelContainer *myIterator;
myIterator = first; // Provided you have an ArtikelContainer called 'first'
myIterator = myIterator->next; // myIterator is now a pointer to the second AC object. 

Generally, an iterator class has the '++' operator overloaded to return the result of the third line.

Do mind that your class definition is wrong: *next has to be a pointer to the next ArtikelContainer, else you're not using a linked list. If you wish to store a linked list of Artikel objects, then the Artikel class needs a Artikel *next pointer, while the ArtikelContainer class needs a Artikel *first pointer.
Oké, thanks. Do you got a full working example maybe?
Topic archived. No new replies allowed.