Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Q3Dict Class Reference
[Qt3Support module]

The Q3Dict class is a template class that provides a dictionary based on QString keys. More...

 #include <Q3Dict>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherits Q3PtrCollection.


Public Functions

Q3Dict ( int size = 17, bool caseSensitive = true )
Q3Dict ( const Q3Dict<type> & dict )
~Q3Dict ()
type * find ( const QString & key ) const
void insert ( const QString & key, const type * item )
bool isEmpty () const
bool remove ( const QString & key )
void replace ( const QString & key, const type * item )
void resize ( uint newsize )
uint size () const
void statistics () const
type * take ( const QString & key )
Q3Dict<type> & operator= ( const Q3Dict<type> & dict )
type * operator[] ( const QString & key ) const

Reimplemented Public Functions

virtual void clear ()
virtual uint count () const

Protected Functions

virtual QDataStream & read ( QDataStream & s, Q3PtrCollection::Item & item )
virtual QDataStream & write ( QDataStream & s, Q3PtrCollection::Item item ) const

Detailed Description

The Q3Dict class is a template class that provides a dictionary based on QString keys.

Q3Dict is implemented as a template class. Define a template instance Q3Dict<X> to create a dictionary that operates on pointers to X (X *).

A dictionary is a collection of key-value pairs. The key is a QString used for insertion, removal and lookup. The value is a pointer. Dictionaries provide very fast insertion and lookup.

If you want to use non-Unicode, plain 8-bit char* keys, use the Q3AsciiDict template. A Q3Dict has the same performance as a Q3AsciiDict. If you want to have a dictionary that maps QStrings to QStrings use QMap.

The size() of the dictionary is very important. In order to get good performance, you should use a suitably large prime number. Suitable means equal to or larger than the maximum expected number of dictionary items. Size is set in the constructor but may be changed with resize().

Items are inserted with insert(); 0 pointers cannot be inserted. Items are removed with remove(). All the items in a dictionary can be removed with clear(). The number of items in the dictionary is returned by count(). If the dictionary contains no items isEmpty() returns TRUE. You can change an item's value with replace(). Items are looked up with operator[](), or with find() which return a pointer to the value or 0 if the given key does not exist. You can take an item out of the dictionary with take().

Calling setAutoDelete(TRUE) for a dictionary tells it to delete items that are removed. The default behavior is not to delete items when they are removed.

When an item is inserted, the key is converted (hashed) to an integer index into an internal hash array. This makes lookup very fast.

Items with equal keys are allowed. When inserting two items with the same key, only the last inserted item will be accessible (last in, first out) until it is removed.

The Q3DictIterator class can traverse the dictionary, but only in an arbitrary order. Multiple iterators may independently traverse the same dictionary.

When inserting an item into a dictionary, only the pointer is copied, not the item itself, i.e. a shallow copy is made. It is possible to make the dictionary copy all of the item's data (a deep copy) when an item is inserted. insert() calls the virtual function Q3PtrCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement newItem() if you want deep copies.

When removing a dictionary item, the virtual function Q3PtrCollection::deleteItem() is called. Q3Dict's default implementation is to delete the item if auto-deletion is enabled.

See also Q3DictIterator, Q3AsciiDict, Q3IntDict, and Q3PtrDict.


Member Function Documentation

Q3Dict::Q3Dict ( int size = 17, bool caseSensitive = true )

Constructs a dictionary optimized for less than size entries.

We recommend setting size to a suitably large prime number (e.g. a prime that's slightly larger than the expected number of entries). This makes the hash distribution better which will lead to faster lookup.

If caseSensitive is TRUE (the default), keys which differ only by case are considered different.

Q3Dict::Q3Dict ( const Q3Dict<type> & dict )

Constructs a copy of dict.

Each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy).

Q3Dict::~Q3Dict ()

Removes all items from the dictionary and destroys it. If setAutoDelete() is TRUE, each value is deleted. All iterators that access this dictionary will be reset.

See also setAutoDelete().

void Q3Dict::clear ()   [virtual]

Reimplemented from Q3PtrCollection::clear().

Removes all items from the dictionary.

The removed items are deleted if auto-deletion is enabled.

All dictionary iterators that operate on the dictionary are reset.

See also remove(), take(), and setAutoDelete().

uint Q3Dict::count () const   [virtual]

Reimplemented from Q3PtrCollection::count().

Returns the number of items in the dictionary.

See also isEmpty().

type * Q3Dict::find ( const QString & key ) const

Returns the item with key key, or 0 if the key does not exist in the dictionary.

If there are two or more items with equal keys, then the most recently inserted item will be found.

Equivalent to the [] operator.

See also operator[]().

void Q3Dict::insert ( const QString & key, const type * item )

Inserts the key key with value item into the dictionary.

Multiple items can have the same key, in which case only the last item will be accessible using operator[]().

item may not be 0.

See also replace().

bool Q3Dict::isEmpty () const

Returns TRUE if the dictionary is empty, i.e. count() == 0; otherwise returns FALSE.

See also count().

QDataStream & Q3Dict::read ( QDataStream & s, Q3PtrCollection::Item & item )   [virtual protected]

Reads a dictionary item from the stream s and returns a reference to the stream.

The default implementation sets item to 0.

See also write().

bool Q3Dict::remove ( const QString & key )

Removes the item with key from the dictionary. Returns TRUE if successful, i.e. if the item is in the dictionary; otherwise returns FALSE.

If there are two or more items with equal keys, then the last item that was inserted will be removed.

The removed item is deleted if auto-deletion is enabled.

All dictionary iterators that refer to the removed item will be set to point to the next item in the dictionary's traversal order.

See also take(), clear(), and setAutoDelete().

void Q3Dict::replace ( const QString & key, const type * item )

Replaces the value of the key, key with item.

If the item does not already exist, it will be inserted.

item may not be 0.

Equivalent to:

 Q3Dict<char> dict;
     ...
 if ( dict.find( key ) )
     dict.remove( key );
 dict.insert( key, item );

If there are two or more items with equal keys, then the last item that was inserted will be replaced.

See also insert().

void Q3Dict::resize ( uint newsize )

Changes the size of the hash table to newsize. The contents of the dictionary are preserved, but all iterators on the dictionary become invalid.

uint Q3Dict::size () const

Returns the size of the internal hash array (as specified in the constructor).

See also count().

void Q3Dict::statistics () const

Debugging-only function that prints out the dictionary distribution using qDebug().

type * Q3Dict::take ( const QString & key )

Takes the item with key out of the dictionary without deleting it (even if auto-deletion is enabled).

If there are two or more items with equal keys, then the last item that was inserted will be taken.

Returns a pointer to the item taken out, or 0 if the key does not exist in the dictionary.

All dictionary iterators that refer to the taken item will be set to point to the next item in the dictionary traversal order.

See also remove(), clear(), and setAutoDelete().

QDataStream & Q3Dict::write ( QDataStream & s, Q3PtrCollection::Item item ) const   [virtual protected]

Writes a dictionary item to the stream s and returns a reference to the stream.

See also read().

Q3Dict<type> & Q3Dict::operator= ( const Q3Dict<type> & dict )

Assigns dict to this dictionary and returns a reference to this dictionary.

This dictionary is first cleared, then each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy), unless newItem() has been reimplemented.

type * Q3Dict::operator[] ( const QString & key ) const

Returns the item with key key, or 0 if the key does not exist in the dictionary.

If there are two or more items with equal keys, then the most recently inserted item will be found.

Equivalent to the find() function.

See also find().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 38
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  5. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
Page suivante

Le Qt Quarterly au hasard

Logo

Le repérage des paires de parenthèses avec QSyntaxHighlighter

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web