Flylib.com

Books Software

 
 
 

Points of Departure


Points of Departure

  1. Visit the Unicode Web site. Explain what it is and why it is important that QString supports the Unicode standard.

  2. Explore the QString documentation. Explain what it means for a string implementation to support the Unicode standard.

  3. Look up make in the documentation. Discuss four useful command line options for the make command.



Review Questions

1.

What is a project file? How can you produce one for your project?

2.

What does the TEMPLATE variable mean in the qmake project file? What are possible values?

3.

What is a Makefile? How can you produce a Makefile for your project?



Chapter 4. Lists

Whenever possible, we use lists in favor of arrays. This chapter explains ways of grouping things together in lists and how to iterate through them.

4.1

Introduction to Containers

96

4.2

Iterators

97

4.3

Relationships

99




4.1. Introduction to Containers

There are many occasions when it is necessary to deal with collections of things. The classic approach in languages like C is to use arrays to store such collections. In C++ arrays are regarded as evil. Here are a few good reasons to avoid using arrays.

  • Array subscripts are not checked to make sure that they are not out of range. A programmer using an array has the responsibility to write extra code to do the range checking.

  • Arrays are either fixed in size or they must use dynamic memory from the heap. With heap arrays, the programmer is responsible for making sure that, under all possible circumstances, the memory gets properly deallocated when the array is destroyed . To do this properly requires deep knowledge of C++, exceptions, and what happens under exceptional circumstances.

  • Inserting, prepending, or appending elements to an array can be expensive operations (in terms of both run time and developer time).

The Standard Library and Qt both provide the programmer with lists that resize themselves as needed and also perform range checking. std::list and QList are each considered basic generic containers in their respective libraries. They are similar to each other in interface (the way they are used from client code), but very different in implementation (the way they behave at runtime).

A generic container is named as such because

  1. Generics are classes or functions that accept template (see Section 10.1) parameters so that they can operate on any type.

  2. Containers (see Section 10.2) are objects that can contain other objects.

To use a QList , the client code must contain a declaration that answers the question: "List of what?" Like other generic containers, QList is a template class (see Chapter 10) and must be declared in the following way.

QList<double> doublList;
QList<Thing> thingList;


QList supports many operations. As with any class you reuse, it is recommended that you scan the API docs to get an overview of its full capabilities. With a single function call, items can be added, removed, swapped, queried, cleared, moved, located, and counted in a variety of ways.



4.2. Iterators

Any time you have a container of things, sooner or later you are probably going to loop through the container and do something with each thing. An iterator is an object that provides indirect access to each element in a container. It is specifically designed to be used in a loop.

Qt 4 supports the following styles of iteration:

  1. Qt 4 style foreach loops , similar to Perl and Python

  2. Java 1.2 style Iterator

  3. Standard Library style ContainerType ::iterator

  4. Hand-made while or for loops that use getters of the container

The next section demonstrates the various styles of iteration available in C++ with Qt 4.

4.2.1. QStringList and Iteration

For text processing, it is very useful to work with lists of strings. QStringList is derived from QList<QString> so it inherits all of QList 's behavior (see Chapter 6). In addition, QStringList has some string-specific convenience functions such as indexOf() , join() , and replaceInStrings() .

Converting between lists and individual strings is quite easy with perl-like split() and join() functions. Example 4.1 demonstrates lists, iterations, split() , and join() .

Example 4.1. src/collections/lists/lists-examples.cpp
#include <QStringList>
#include <QDebug>
#include <cassert>

/* Some simple examples using QStringList, split and join */
int main() {

    QString winter = "December, January, February";
    QString spring = "March, April, May";
    QString summer = "June, July, August";
    QString fall = "September, October, November";

    QStringList list;
    list << winter;

<-- 1

list += spring;

<-- 2

list.append(summer);

<-- 3

list << fall;

    qDebug() << "The Spring months are: " << list[1] ;

    QString allmonths = list.join(", ");
    /* from list to string - join with a ", " delimiter */
    qDebug() << allmonths;

    QStringList list2 = allmonths.split(", ");
    /* split is the opposite of join. Each month will have its
    own element. */

    assert(list2.size() == 12);

<-- 4

foreach (QString str, list)  {

<-- 5

qDebug() << QString(" [%1] ").arg(str);
    }

    for (QStringList::iterator it = list.begin();
         it != list.end(); ++it) {

<-- 6

QString current = *it;

<-- 7

qDebug() << "[[" << current << "]]";
    }

    QListIterator<QString> itr (list2);

<-- 8

while (itr.hasNext()) {

<-- 9

QString current = itr.next();
        qDebug() << "{" <<  current << "}";
    }

    return 0;
}

(1) append operator 1

(2) append operator 2

(3) append member function

(4) Assertions abort the program if the condition is not satisfied.

(5) Qt 4 foreach loop, similar to Perl/Python and Java 1.5 style for loops.

(6) C++ STL-style iteration

(7) pointer-style dereference

(8) Java 1.2 style iterator

(9) Java iterators point in between elements.

src/collections> ./collections
The Spring months are: March, April, May
December, January, February, March, April, May, June, July, August,
September, October, November
 [December]
 [January]
 [February]
 [March]
 [April]
 [May]
 [June]
 [July]
 [August]
 [September]
 [October]
 [November]
src/collections>