Qt core programming ___ collection class

Source: Internet
Author: User

QtCore programmingCollection classIs the content to be introduced in this article.QtWe will introduce the core programming in several parts. For more information, see the editorial recommendations at the end for detailed reading. First, let's take a look at this article.

OneCollection classIs a container with multiple entries. Each entry is a data structure,Collection classAllows you to insert, delete, and search for entries in a container.

QtThere are several value-based and pointer-basedCollection class. Pointer-basedCollection classWorks with pointers pointing to entries, while value-basedCollection classStores copies of the entries. Value-basedCollection classSimilar to the STL container class, it can be used with STL algorithms and containers.

Descriptions of value-based collection classes are shown in table 4:

Table 4 value-based collection tables

Table 5 describes pointer-based collection classes:

Table 5 pointer-based collection tables

Qmemarray is an exception. It is neither pointer-based nor value-based, but memory-based. Qmemarray is used in arrays with simple data structures with the highest efficiency. qmemarray uses bitwise logical operators for copying and comparing array elements.

There are some iterators in these classes, and the iterator is the class that traverses entries in the Collection class. In the QT template library, value-based sets and algorithms are integrated. The following describes pointer-based containers.

1. pointer-based container structure

Pointer-based containers have four internal base classes (qgcache, qgdict, qglist, and qgvector) to operate void type pointers. By adding/deleting entry pointers, a thin template layer composed of the four classes implements the actual set.

The policy that allows the QT template class makes the space very economical (implementing these template classes only adds inline calls to the base class), and does not affect the execution efficiency.

Example: qptrlist

The following example shows how to store the employee entries to a linked list and print them in reverse order.

  1. # Include <qptrlist. h>
  2. # Include <qstring. h>
  3. # Include <stdio. h>
  4. Class employee {public: employee (const char * Name, int salary ){
  5. N = Name; S = salary;
  6. }
  7. Const char * Name () const {
  8. Return N;
  9. }
  10. Int salary () const {
  11. Return S;
  12. }
  13. PRIVATE: qstring N;
  14. Int S;
  15. };
  16. Int main (){
  17. Qptrlist <employee> list; // pointer linked list pointing to employee
  18. List. setautodelete (true); // delete an entry when the Linked List entry is moved.
  19. List. append (new employee ("bill", 50000); // append a new object to the linked list
  20. List. append (new employee ("Steve", 80000 ));
  21. List. append (new employee ("Ron", 60000 ));
  22. Qptrlistiterator <employee> it (list); // traverses the employee linked list
  23. For (it. tolast (); it. Current (); -- It) {// traverse from the end to the header
  24. Employee * EMP = it. Current ();
  25. Printf ("% s earns % d \ n", EMP-> name (), EMP-> salary ());
  26. }
  27. Return 0;
  28. }

The program running result is as follows:

  1. Ron earns 60000
  2. Steve earns 80000
  3. Bill earns 50000

2. Manage collection entries

All pointer-based sets inherit the qptrcollection base class. This class only knows the number of entries in the Set and the deletion policy.

When entries in the set are removed, they are not deleted due to lack of time. Qptrcollection: setautodelete () defines the deletion policy. In the above qptrlist example, we have activated the automatic delete function to delete the linked list.

When an entry is inserted into a set, only the pointer is copied, rather than the entry itself. This is called a shortest copy. When an entry is inserted, it is also possible to copy the array of all entries to the set, which is called Deep copy.

All collection class functions call the virtual function qptrcollection: newitem () when inserting an entry (). If you want to make a deep copy, you need to reload it.

When an entry is removed from a linked list, the virtual function qptrcollection: deleteitem () is called (). If the automatic deletion function is activated, the default implementation function in all collection classes is called to delete entries.

Pointer-based collection classes, such as qptrlist <type>, define a collection of pointers to objects. We will only discuss the qptrlist class here. Other pointer-based collection classes and all collection class iterators share the same usage method.

The template instantiation method is as follows:

  1. Qptrlist <employee> list;

In this example, the category or type of an entry is employee, which must be defined before the linked list is defined. For example:

  1. Class employee {...};
  2. Qptrlist <employee> list;

3. iterators)

Qptrlistiterator can be used to modify a linked list and ensure that the linked list is safe. Multiple iterators can work independently on the same set.

Qptrlist has an internal linked list pointing to all iterators that currently operate on the linked list. When a linked list entry is moved, the linked list updates all iterator pointing to this entry.

The qdict and qcache sets have no traversal functions. To traverse a set, you must use qdictiterator or qcacheiterator.

QtPredefinedCollection classThere are string linked lists: qstrlist, qstrilist (in qstrlist. H), and qstringlist (in qstringlist. h ). In most cases, you will select qstringlist, which is a linked list of shared qstring Unicode strings. Qptrstrlist and qptrstrilist only store character pointers, not strings themselves.

Pointer-basedCollection classAnd Related iterator classes are described in table 4.

Table 4 pointer-basedCollection classList of related iterator classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.