Welchen Container verwenden

Du bist neu in der Welt von C++? Dann schau hier herein!
Antworten
jd
Beiträge: 130
Registriert: 22. Januar 2008 17:55

Welchen Container verwenden

Beitrag von jd »

Hallo Gemeinde,

Ich stehe vor einer kleinen Entscheidungsfrage und zwar welchen Container ich verwenden sollte, wozu ich erstmal klären muss, welcher Container für was da ist. Hier mal ein kleins Beispiel.

Code: Alles auswählen

typedef struct {
  QString test;
  bool foo;
  int bar;
}SFooStruct;
Wenn ich jetzt mehrere Instanzen von der Struktur bilde, und die abspeichern will, welchen Container verwenden man da am besten? Primär geht es mit um:

Vector
List
Hashmap

Was sind die Eigenschaften von Vector, List und Hashmap? Kann mir da jemand weiter helfen?
CaptnChaos
Beiträge: 605
Registriert: 28. Juni 2007 15:01
Kontaktdaten:

Beitrag von CaptnChaos »

QList<T>
This is by far the most commonly used container class. It stores a list of values of a given type (T) that can be accessed by index. Internally, the QList is implemented using an array, ensuring that index-based access is very fast.
Items can be added at either end of the list using QList::append() and QList::prepend(), or they can be inserted in the middle using QList::insert(). More than any other container class, QList is highly optimized to expand to as little code as possible in the executable. QStringList inherits from QList<QString>.
QLinkedList<T>
This is similar to QList, except that it uses iterators rather than integer indexes to access items. It also provides better performance than QList when inserting in the middle of a huge list, and it has nicer iterator semantics. (Iterators pointing to an item in a QLinkedList remain valid as long as the item exists, whereas iterators to a QList can become invalid after any insertion or removal.)
QVector<T>
This stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of a vector can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory.
QStack<T>
This is a convenience subclass of QVector that provides "last in, first out" (LIFO) semantics. It adds the following functions to those already present in QVector: push(), pop(), and top().
QQueue<T>
This is a convenience subclass of QList that provides "first in, first out" (FIFO) semantics. It adds the following functions to those already present in QList: enqueue(), dequeue(), and head().
QSet<T>
This provides a single-valued mathematical set with fast lookups.
QMap<Key, T>
This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn't matter QHash is a faster alternative.
QMultiMap<Key, T>
This is a convenience subclass of QMap that provides a nice interface for multi-valued maps, i.e. maps where one key can be associated with multiple values.
QHash<Key, T>
This has almost the same API as QMap, but provides significantly faster lookups. QHash stores its data in an arbitrary order.
QMultiHash<Key, T>
This is a convenience subclass of QHash that provides a nice interface for multi-valued hashes.
Antworten