Qt学习之路—containers(1)

August 7th, 2013 by JasonLe's Tech Leave a reply »

今天学习了QList<T>  list 类 与 迭代器QListIterator<T>     QMutableListIterator<T>  iter(list)

QMutableListIterator<T> allows you to iterate over a QList<T> (or a QQueue<T>) and modify the list. If you don’t want to modify the list (or have a const QList), use the slightly faster QListIterator<T> instead.

QMutableListIterator::QMutableListIterator ( QList<T> & list )

Constructs an iterator for traversing list. The iterator is set to be at the front of the list (before the first item).

bool QMutableListIterator::findNext ( const T & value )

Searches for value starting from the current iterator position forward. Returns true if value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just after the matching item; otherwise, the iterator is positioned at the back of the container.

See also findPrevious().

bool QMutableListIterator::findPrevious ( const T & value )

Searches for value starting from the current iterator position backward. Returns true if value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just before the matching item; otherwise, the iterator is positioned at the front of the container.

See also findNext().

bool QMutableListIterator::hasNext () const

Returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns false.

See also hasPrevious() and next().

bool QMutableListIterator::hasPrevious () const

Returns true if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns false.

See also hasNext() and previous().

void QMutableListIterator::insert ( const T & value )

Inserts value at the current iterator position. After the call, the iterator is located just after the inserted item.

See also remove() and setValue().

T & QMutableListIterator::next ()

Returns a reference to the next item, and advances the iterator by one position.

Calling this function on an iterator located at the back of the container leads to undefined results.

See also hasNext(), peekNext(), and previous().

void QMutableListIterator::remove ()

Removes the last item that was jumped over using one of the traversal functions (next(), previous(), findNext(), findPrevious()).

Qt的容器类为遍历其中的操作提供了两种方法:
1.java风格的迭代器,
2。STL风格的。

下面介绍STL风格
QList QList::const_iterator QList::iterator///中间那个是只读的,最后是可读写的
QLinkedList QLinkedList::const_iterator QLinkedList::iterator

QList<int> list;
for(int j=0; j<10; j++)
  list.insert(list.end(), j);//表示在列表的最后插入直j
QList<int>::iterator i;
  for(i=list.begin(); i!=list.end(); ++i)
  {
  qDebug() << (*i);
  }
QList<int>::const_iterator ci;
  for(ci=list.constBegin(); ci!=list.constEnd(); ++ci)
  qDebug() << "const "<<*ci;
QMutableListIterator<int> Lter(list);
  while(Lter.hasNext())
  {
  if(Lter.next()==5)
  {
  Lter.remove();
  }
 }

下面来讲解一下qSort()的用法。

qSort()包含在<QtAlgorithms>中,其中主要包含有

http://harmattan-dev.nokia.com/docs/library/html/qt4/qtalgorithms.html#qSort

1.void qSort ( RandomAccessIterator.begin(), RandomAccessIterator.end() )

2.void qSort ( RandomAccessIterator.begin(), RandomAccessIterator.end(), LessThan lessThan )

3.void qSort ( Container ; container )

3的函数相当与第一个函数 container.begin(),container.end().

第一个函数特殊之处在于,可以指定是哪几个index的数列之间进行排序。例如 qSort(container.begin()+1,container.end()-1);其中container is a certain container such as list Linklst ……..上个函数的就是排序第二个到倒数第二个之间的数据。