QT Container Class

Source: Internet
Author: User
Tags documentation

QT not only supports C + + STL Template Library, but also defines a set of container class and the operation of the algorithm class, using QT defined this set of libraries, you can make the same performance on each platform. These containers of QT are designed to be lighter, safer and easier to use. The container class is implicitly shared (implicitly), Reentrant (reentrant), and thread-safe. Provides two types of iterators (Java-style,stl-style) for container traversal, and QT provides a foreach keyword for more convenient element traversal.
Continuous Container:

Qvector<t>

It is the most common container class in QT, it corresponds to the vector<t> container in the STL, is the dynamic array, provides the fast index access.

The qlist<t> qlinklist<t> qvarlengtharray<t> provides similar functionality.

You can refer to the help documentation to see how the Qlist is used, and use the following section for examples:

1. Create various types of vectors:

Qvector<int> Integervector;

Qvector<qstring> Stringvector;

2. Vector to create a certain number of items

Qvector<qstring> vector (200);

3. Create vector with initialization

Qvector<qstring> Vector ("pass");

You can also use fill to assign values:

Qvector<qstring> vector (3);

Vector.fill ("Yes");

Vector: ["yes", "yes", "yes"]

Vector.fill ("Oh", 5);

Vector: ["Oh", "Oh", "Oh", "Oh", "Oh"]

The 4.QVector provides the [] subscript operation as a vector of C + +, starting with 0 items. A read-only operation for at () is also provided, and at () is faster than [] because it does not cause deep replication.

if (vector[0] = = "Liz")

Vector[0] = "Elizabeth";

for (int i = 0; i < vector.size (); ++i) {

if (vector.at (i) = = "Alfonso")

cout << "Found Alfonso at position" << I << Endl;

}

5. You can use Indexof,lastindexof to query for an index that gets a value:

Qvector<qstring> Vector;

Vector << "A" << "B" << "C" << "B" << "a";

Vector.indexof ("B"); Returns 1

Vector.indexof ("B", 1); Returns 1

Vector.indexof ("B", 2); Returns 3

Vector.indexof ("X"); Returns-1

Vector.lastindexof ("B"); Returns 3

Vector.lastindexof ("B", 3); Returns 3

Vector.lastindexof ("B", 2); Returns 1

Vector.lastindexof ("X"); Returns-1

You can also use contains () to see if an element is included and to return a bool value.

6. Add elements by Append,operator<<,prepend,insert. (for larger vectors, inserting items at both the beginning and the middle is time-consuming.) This situation is more suitable for using qlinkedlist<t>)

Qvector<qstring> vector (0);

Vector.append ("one");

Vector.append ("two");

Vector.append ("three");

Vector: ["One", "two", "three"]

Qvector<qstring> vector (0);

Vector << "One" << "two" << "three";

Vector: ["One", "two", "three"]

Qvector<qstring> Vector;

Vector.prepend ("one");

Vector.prepend ("two");

Vector.prepend ("three");

Vector: ["Three", "two", "one"]

Qvector<qstring> Vector;

Vector << "alpha" << "beta" << "Delta";

Vector.insert (2, "gamma");

Vector: ["alpha", "beta", "gamma", "Delta"]

7.size () Resize () IsEmpty () capacity () and container size related operations.

8. Related transformation: ToList () Tostdvector ()

Qvector<double> Vect;

Vect << "Red" << "green" << "Blue" << "black";

qlist<double> list = Vect.tolist ();

List: ["Red", "green", "Blue", "black"]

Qvector<double> Vector;

Vector << 1.2 << 0.5 << 3.14;

std::vector<double> stdvector = Vector.tostdvector ();

(The use of the following container action functions will no longer be described, and the help documentation is consulted, and the qvector is used the same way.) )

Qlinkedlist<t>

As mentioned earlier, it is suitable for random inserts, because of its chained structure. He provides a constant-time insertion deletion, but does not provide fast random access operations. does not provide a [] operation, its traversal element is accomplished through an iterator.

Qlist<t>

It is a list of arrays, combining the advantages of the above two structures, it supports random access, and inserts and deletes at any end of it are very fast and in the middle of the list, inserts and deletes are also quick. Those who have studied the data structure will clearly understand the structural differences between the three. If you want each item element to be contiguous, you can only use Qvector.

Qstring<t>

It is a subclass of Qlist<qstring>, which provides a more general operation for string manipulation.

Qstack<t> qqueue<t>

They are implementations of the stack and queue structure, and Qstack provides the pop () push () swap () Top () operation, which inherits from the Qvector<t>

Qqueue<t> provides dequeue () Enqueue () head () swap operation. Inherited from Qlist<t>.
Associative Container

Qset<t>

It provides a set of key-value pairs that can be quickly searched,

Qmap<key, t> Qmultimap<key, t>

Qmap is a data structure that stores key-value pairs in ascending order, Qmultimap is qmap based on a maps that can store multiple values, which means that a key corresponds to multiple values.

The following is the creation of a qstring-int maps

Qmap<qstring, int> map;

You can insert a value like this

map["one"] = 1;

Map["three"] = 3;

Map["Seven"] = 7;

You can also do this:

Map.insert ("Twelve", 12);

Query for a value using [] or value ("* *")

int NUM1 = map["Thirteen"];

int num2 = Map.value ("Thirteen");

Whether a query has a value:

if (Map.contains ("TIMEOUT"))

Timeout = map.value ("timeout");

It is generally recommended to use contains () value () instead of [].

Qhash<key, t> Qmultihash<key, t>

Qhash<key, t> is a structure that stores key-value pairs in a hash table. Its interface is almost the same as QMAP, but it provides a faster lookup capability.

Qhash automatically assigns the original storage area to its internal hash table and then divides the allocated area size when an item is inserted or deleted. You can also call reserve () or squeeze () to specify or compress the number of items that you want to store in the hash table for performance tuning. The usual practice is to call the reserve () with the number of the largest items we expect, then insert the data, and then call Squeeze () to minimize the memory if there are more items.
iterators

For each container there are two styles of iterator--java style and STL style. The Java style is easier to use with a very small amount of performance as the cost, and STL style can be combined with STL algorithm to be more powerful.

Here we'll give an example of Qlist and Qmap iterators.


Java-style:

Java-style iterators are divided into two types: read-only iterators, read-write iterators. Read-only iterators are q*iterator<t> (for example, qvectoriterator<t>), while read-write iterators are like qmutable*iterator<t> This (e.g.:qmutablevectoriterator<t>).

Containers

Read-only iterator

Read-write iterator

Qlist<t> a qqueue<t>.

Qlistiterator<t>

Qmutablelistiterator<t>

Qlinkedlist<t>

Qlinkedlistiterator<t>

Qmutablelinkedlistiterator<t>

Qvector<t> a qstack<t>.

Qvectoriterator<t>

Qmutablevectoriterator<t>

Qset<t>

Qsetiterator<t>

Qmutablesetiterator<t>

Qmap<key, T>, Qmultimap<key, t>

Qmapiterator<key, t>

Qmutablemapiterator<key, t>

Qhash<key, T>, Qmultihash<key, t>

Qhashiterator<key, t>

Qmutablehashiterator<key, t>

Valid locations for Java-style iterators:


Here is a typical example of use:

Qlist<qstring> list;

List << "A" << "B" << "C" << "D";

qlistiterator<qstring> I (list);

while (I.hasnext ())

Qdebug () << i.next ();

The following shows how to traverse backwards

qlistiterator<qstring> I (list);

I.toback ();

while (I.hasprevious ())

Qdebug () << i.previous ();

If there is an item on the left, then Hasprevious () returns True. Previous () returns the item to the left of the iterator and moves one position forward. You can look at the picture as follows:


The following table is the Qlistiterator API and description

Function

Behavior

Tofront ()

The iterator moves to the front, before the first

Toback ()

The iterator moves to the end, after the last item

Hasnext ()

Returns true if it is not the last of the list

Next ()

Returns the next item, and the iterator moves backward one

PeekNext ()

Returns the next item, the iterator does not move

Hasprevious ()

Returns true if it is not the top of the list

Previous ()

Returns the previous item, and the iterator moves backward one

Peekprevious ()

Returns the previous item, the iterator does not move

The following are instructions for using the mutable iterator read-write iterator:

Qlist<int> Remove Cardinality entry:

qmutablelistiterator<int> I (list);

while (I.hasnext ()) {

if (I.next ()% 2!= 0)

I.remove ();

}

The following is an example of the Qmap iterator, which is similar to the previous one:

Qmap<qstring, qstring> map;

Map.insert ("Paris", "France");

Map.insert ("Guatemala City", "Guatemala");

Map.insert ("Mexico City", "Mexico");

Map.insert ("Moscow", "Russia");

...

Qmutablemapiterator<qstring, qstring> I (map);

while (I.hasnext ()) {

if (I.next (). Key (). EndsWith ("City")

I.remove ();

}

Qmap<int, Qwidget *> map;

Qhash<int, Qwidget *> hash;

Qmapiterator<int, Qwidget *> i (map);

while (I.hasnext ()) {

I.next ();

Hash.insert (I.key (), I.value ());

}


Stl-style:

STL style is the general algorithm that the iterator not only supports QT, but also is compatible with STL.

Similar to the Java style, it also has two styles of iterators, read-only (const_iterator) and read-write (iterator).

Containers

Read-only iterator

Read-write iterator

Qlist<t> a qqueue<t>.

Qlist<t>::const_iterator

Qlist<t>::iterator

Qlinkedlist<t>

Qlinkedlist<t>::const_iterator

Qlinkedlist<t>::iterator

Qvector<t> a qstack<t>.

Qvector<t>::const_iterator

Qvector<t>::iterator

Qset<t>

Qset<t>::const_iterator

Qset<t>::iterator

Qmap<key, T>, Qmultimap<key, t>

Qmap<key, T>::const_iterator

Qmap<key, T>::iterator

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.