IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

iterator Class

The https:// class provides an STL-style non-const iterator for https://.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

iterator Class▲

  • Header: iterator

  • qmake: QT += core

Detailed Description▲

https:// features both https:// and https://. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity.

https://<T>::iterator allows you to iterate over a https://<T> and to modify the list item associated with the iterator. If you want to iterate over a const https://, use https:// instead. It is generally good practice to use https:// on a non-const https:// as well, unless you need to change the https:// through the iterator. Const iterators are slightly faster, and can improve code readability.

The default https:// constructor creates an uninitialized iterator. You must initialize it using a function like https://(), QLinkedList::end(), or QLinkedList::insert() before you can start iterating. Here's a typical loop that prints all the items stored in a list:

 
Sélectionnez
QLinkedList&lt;QString&gt; list;
list.append("January");
list.append("February");
...
list.append("December");

QLinkedList&lt;QString&gt;::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    cout &lt;&lt; *i &lt;&lt; endl;

STL-style iterators can be used as arguments to https://. For example, here's how to find an item in the list using the qFind() algorithm:

 
Sélectionnez
QLinkedList&lt;QString&gt; list;
...
QLinkedList&lt;QString&gt;::iterator it = qFind(list.begin(),
                                          list.end(), "Joel");
if (it != list.end())
    cout &lt;&lt; "Found Joel" &lt;&lt; endl;

Let's see a few examples of things we can do with a https:// that we cannot do with a https://. Here's an example that increments every value stored in a https://<int> by 2:

 
Sélectionnez
QLinkedList&lt;int&gt;::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    *i += 2;

Here's an example that removes all the items that start with an underscore character in a https://<QString>:

 
Sélectionnez
QLinkedList&lt;QString&gt; list;
...
QLinkedList&lt;QString&gt;::iterator i = list.begin();
while (i != list.end()) {
    if ((*i).startsWith('_'))
        i = list.erase(i);
    else
        ++i;
}

The call to https://() removes the item pointed to by the iterator from the list, and returns an iterator to the next item. Here's another way of removing an item while iterating:

 
Sélectionnez
QLinkedList&lt;QString&gt;::iterator i = list.begin();
while (i != list.end()) {
    QLinkedList&lt;QString&gt;::iterator previous = i;
    ++i;
    if ((*previous).startsWith('_'))
        list.erase(previous);
}

It might be tempting to write code like this:

 
Sélectionnez
// WRONG
while (i != list.end()) {
    if ((*i).startsWith('_'))
        list.erase(i);
    ++i;
}

However, this will potentially crash in ++i, because i is a dangling iterator after the call to https://().

Multiple iterators can be used on the same list. If you add items to the list, existing iterators will remain valid. If you remove items from the list, iterators that point to the removed items will become dangling iterators.

Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read https://.

See Also▲

See also https://, QMutableLinkedListIterator

Member Function Documentation▲

 

iterator::iterator()▲

Constructs an uninitialized iterator.

Functions like operator*() and operator++() should not be called on an uninitialized iterator. Use operator=() to assign a value to it before using it.

See Also▲

See also https://(), QLinkedList::end()

iterator::iterator(const iterator &other)▲

Constructs a copy of other.

[default] QLinkedList<T>::iterator &iterator::operator=(const QLinkedList::iterator &other)▲

Assigns other to this iterator.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.