Qt container class

Source: Internet
Author: User
Tags sprintf

Sequentail containers
Qvector <t>

Array-like data structure (data insertion at the end has a high validity rate, while data insertion at the center and the header has a high overhead ).
Qvector provides [] Operators
Qvector can replace the append () function with the <operator.
The basic type and pointer in qvector are initialized to 0.
Qinilist <t>

Qinilist does not provide the [] Operator, so it must be traversed by the iterator.
Qlist <t>
Array-list: combines qvector <t> and qw.list <t>. The most important advantage is that the [] operator is supported.
The insert/delete operation at the header or tail is very fast, and the insert/delete operation in the middle is very fast when the size is less than 1000.
Generally, qlist is the most suitable general-purpose container.
Qstringlist
: A subclass of qlist <qstring>, which is widely used in QT APIs.
Qstack <t> and qqueue <
T> is two convenience provided by QT
Subclasses, qstack <t> is actually an additional qvector that provides the push (), pop () Top () interface, while
Qqueue <t> is actually an additional qlist that provides enqueue (), dequeue (), and head.
Classes that can be placed in the container must have default constructor, copy constructor, and assignment operator (explicitly defined or generated by the compiler)
Note that the class derived from qobject does not meet the preceding requirements because it does not have copy constructor and assignment operator. The solution is to store the object pointer in the container rather than the object itself.
The elements stored in the container can also be containers, that is, they can be nested-but you must note that the continuous angle brackets are separated by spaces to avoid the compiler mistaken for the> operator.
Iterator

Qt supports two types of iterators --
Java-style and STL-
Style


Java-style iterators are easier to use, while STL-style iterators can be used together with algorithms in QT and STL, making them more powerful.
Java-style iterator

Each sequential container class has two Java-style iterator types: Read-Only iterator and read/write iterator.

When using the Java-style iterator, the first thing to be clear is that the iterator does not directly point to the elements in the container, but to the positions before or after the elements. The iterator points
Before the first element in the container; if an element exists on the right of the iterator, The hasnext () function returns true; the next () function returns the element on the right of the iterator, and move the iterator to the right
Moves the position of an element. The hasprevious () and previous () functions perform operations in the opposite direction.
The remove () function always deletes the last skipped element.
The setvalue () function always performs an update operation on the last skipped element.
The insert () function inserts a new element at the position currently pointed by the iterator, and points the iterator to the position between the new element and its subsequent elements.
STL-style iterator

Each sequential container class has two STL-style iterator types: Container <t >:: iterator and container <t >:: const_iterator.

The begin () function of the container returns an iterator pointing to the Header element in the container, and end () returns the iterator pointing to the position after the tail element in the container;
When the container is empty, the results of begin () and end () are the same.
Generally, you can call isempty () to check whether the container is empty, instead of comparing the results of in () and end.
You can use the +,-, and * operators for iterator of STL-style, similar to pointer usage.

The returned values of some QT functions are the container class. If you need to use the STL-style iterator to traverse the returned values, you must save a copy of the returned values and traverse the copies, otherwise, the so-called "dangling iterator" may occur ".
Note: If you use a read-only iterator of Java-style, the replication is completed implicitly to ensure that the iterator is always traversed on the copy.
Implicit sharing (copy on write)

The beauty of the implicit sharing mechanism in QT is that it encourages programmers to use the concise method of passing values when returning objects, rather than referencing or pointers.
In contrast, STL encourages programmers to use non-const references to transmit vectors to avoid overhead of copying function return values.
All containers in QT use the implicit sharing mechanism. In addition, many other classes such as qbytearray, qbrush, qfont, qimage, qstring also uses this mechanism-this ensures that these classes are highly efficient in passing values, whether as a parameter or function return value.
Using the at () rather than the [] operator to perform read-only operations on a vector or list under the implicit sharing mechanism provided by QT is a better choice.
Similarly, try to use constbegin () and constend () to avoid unnecessary copy operations.
Foreach syntax

When entering the loop body, foreach automatically copies the container and iterates on this copy. Therefore, if the container is modified by the iterator In the iteration process, the loop will not be affected, the container content does not change after the loop ends.
Of course, if you directly use the [] operator to write the container in the foreach loop, the container content will naturally change.
The break and continue statements are supported in foreach.
Associative containers

Qmap

Key-value pairs in qmap are sorted in ascending order.
The [] operator can be used in both insert and delete operations, and its subscript is key. To avoid unnecessary null values, we recommend that you use vlaue () instead of [] to obtain values from qmap.
In addition to the default constructor, copy constructor, and value assignment operator, K in qmap <K, T> must also support operator <, in this way, the ascending order mentioned above can be achieved.
Keys () & values ()
Qmap features a single value. qmultimap <K, T> supports the existence of multiple values under the same keyword. The insert operation is completed by insertmulti ().
Qhash

Qhash interfaces are similar to qmap interfaces.
Qhash <K, T> must meet the additional requirements: Operator = is supported, and K can use the global function qhash () to calculate the hash value.
Qhash is usually a single value, while qmultihash supports multi-value insertion through insertmulti.
Qcache <K, T> & qset <k>

The simplest way to traverse associative containr is to use a Java-style iterator.
Foreach syntax can also be used in assocaitive container
Generic Algorithms

The header file <qalgorithms> declares a set of global template functions to implement the basic algorithms acting on containers. Most of the algorithms are completed using the STL-style iterator.
STL header file <algorithm>
Function in to act on the qt container, or the STL container.
Qfind (), qbinaryfind (), qfill (), qcopy (), qsort (), qstablesorg (), qdeleteall (), qswap ()
It should be noted that qdeleteall () only makes sense to containers containing pointers. This function will release all objects, but will not delete pointers in containers.
Strings, byte arrays, and variants

Qstring, qbytearray, and qvariant have many similarities with the container class, which can be used as a substitute for the container class in some cases. Like the container class, these three classes also apply the implicit sharing mechanism.
Qstring

Qstring stores the 16-bit unicold value.
In concept, qstring can be regarded as qvector <qchar>.
Qstring provides the ++ = Operator and the append () function to merge strings.
The sprintf () function provided by qstring supports the same parameter format as sprintf in the C ++ standard library.
The ARG () function provided by qstring is a better choice than sprintf () because it ensures type security and supports Unicode.
Qstring: Number (): Number-> string
Qstring: setnum (): Number-> string
Reverse conversion interfaces: toint (), tolonglong (), todouble (), and so on. These functions all have an optional parameter -- a pointer of the bool type, if the conversion fails, set the bool variable to true; otherwise, set it to false.
The mid () function returns the substrings within the specified range. The left () and right () functions return the Left and Right substrings respectively.
The indexof () function of qstring can be used for text matching. The initial subscript of the matched substring is returned. If the matching fails, the return value is-1.
Startswith () and endswith () functions can be used to determine whether the header and tail of a string conform to a certain pattern.
Qstring is case-sensitive during comparison. When the strings to be compared are visible to users, localeawarecompare () is usually the correct choice.
Tolower () & toupper ()
Replace () & remove () & insert ()
Trimmed () & Simplified () -- these two functions are used to remove whitesapce (spaces, tabs, newlines, etc.) from the string)
Qstring: Split () Splits a string into substrings and returns a qstringlist composed of these substrings.
All elements in the qstringlist can form a new string through the join () function, and the join () parameters are inserted in the middle of Adjacent Elements During the merge.
In most cases, the conversion from const char * To qstring is automatic.
The inverse conversion can be completed through toascii () or tolatin1 (). These functions return a qbytearray.Qbytearray
: Data or qbytearray: constdata () to convert to const char *;
To simplify the conversion, QT provides a qprintable () macro for the same operations as toascii () and constdata.
Qbytearray
The APIS provided by qbytearray are similar to those provided by qstring.
Qbytearray is used to store original binary data and 8-bit encoded strings.

We usually choose qstring instead of qbytearray to store text information, because qstring supports Unicode.
Qbytearray automatically adds '/0' after the last element, which makes it easy to pass qbytearray to the function that requires const char.
Qbytearray supports '/0' and can store any binary data.
Qvariant

The qvariant class can be used to store many QT values and containers.
Qvariant is widely used in item View class, database model, and qsetting.
By using qvariant and nesting, you can create very complex data structures.
The convenience of qvariant is at the cost of performance and code readability.
Qvariant is used by qt's meta-object system, so it is an integral part of the qtcore module.
Qvariant can also support user-defined data types, provided that they have defalut constructor and copy constructor. To support user-defined types, use the macro q_declare to register these types.
Global functions: qvariantfromvalue (), qvariantvalue <t> (), qvariantcanconvert <t> ()

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.