簡介
QList<T>是Qt的一種泛型容器類。它以鏈表方式儲存一組值,並能對這組資料進行快速索引,還提供了快速插入和刪除等操作。
詳解
QList以鏈表形式儲存一組元素。預設為空白鏈表,我們可以使用<<操作符添加元素:
QList<QString> list;
list << "one" << "two" << "three"; // list: ["one", "two", "three"]
對於非const鏈表,操作符[]返回的是該元素的引用,並且傳回值可以用途左運算元。
if ( list[0] == "Bob" )
list[0] = "Robert";
對於唯讀訪問,可以用at()函數實現訪問,at()操作比操作符[]更快,因為它不需要深度複製。
for ( int i=0; i!=list.size(); ++i )
{
if ( list.at(i) == "Jane" )
{ cout << "Found Jane at position:" << i<< endl;}
}
QList在鏈表兩端都預先分配了緩衝,這樣就使得在QList的兩端插入或刪除元素變得非常地快。如果需要找出某個值在鏈表中出現的位置,可以使用indexOf()、lastIndexOf()函數來實現。前者進行正向對應,而後者則進行反向尋找。兩者都會在找到匹配元素後返回該元素的索引值,沒有找到匹配元素返回-1。例如:
int i = indexOf("Jane");
if ( i!=-1 )
cout << "First occurance of Jane is at position" << i << endl;
執行個體解析
QList<T> 釋放記憶體執行個體詳解:
1.T的類型為非指標,這時候直接調用clear()方法就可以釋放了,看如下測試代碼
#include <QtCore/QCoreApplication>
#include <QList>
#include <QString>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
typedef struct _test
{
int id;
QString name;
QString sex;
}Por_test;
QList<Por_test> slist;
for (int i=0;i<100000;i++)
{
Por_test s;
s.id = 1;
s.name = QString("hello World!");
s.sex = QString("男");
slist.append(s);
}
slist.clear();
return a.exec();
}
2.T的類型為指標的情況,這時候直接調用clear()方法將不能釋放,先看代碼
#include <QtCore/QCoreApplication>
#include <QList>
#include <QString>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
typedef struct _test
{
int id;
QString name;
QString sex;
}Por_test;
QList<Por_test *> slist;
for (int i=0;i<100000;i++)
{
Por_test *s = new Por_test();
s->id = 1;
s->name = QString("hello World!");
s->sex = QString("男?");
slist.append(s);
}
qDeleteAll(slist);
slist.clear();
return a.exec();
}
說明當T的類型為指標時,調用clear()方法並不能釋放其記憶體,此時void qDeleteAll ( const Container & c )方法將派上用場了。
我們再來看下qt助手中qDeleteAll 方法的說明
void qDeleteAll ( ForwardIterator begin, ForwardIterator end )
Deletes all the items in the range [begin, end) using the C++ delete operator. The item type must be a pointer type (for example, QWidget *).
Example:
QList<Employee *> list;
list.append(new Employee("Blackpool", "Stephen"));
list.append(new Employee("Twist", "Oliver"));
qDeleteAll(list.begin(), list.end());
list.clear();
Notice that qDeleteAll() doesn't remove the items from the container; it merely calls delete on them. In the example above, we call clear() on the container to remove the items.
This function can also be used to delete items stored in associative containers, such as QMap and QHash. Only the objects stored in each container will be deleted by this function; objects used as keys will not be deleted.
void qDeleteAll ( const Container & c )
This is an overloaded member function, provided for convenience.
This is the same as qDeleteAll(c.begin(), c.end()).
注意事項
QList<T>中,如果T<=4的話,QList會直接將T存放在為其開闢的記憶體中,如果T>4的話,QList中存放指向T的指標。例如:存放penson類,包括姓名,行動電話,家庭電話等資訊,QList<penson> contactList 使用指標是取不到期望的值,直接用contactList[i]就行,而且用成員函數at()返回的值是const,不能進行修改。
QList的值必須是可數的類型,這包含了我們常用的大多數類型。如果我們儲存QWidget類型的話,應該使用QWidget *,而不是QWidget。
indexOf()和lastIndexOf()操作要求值的類型能進行“==”操作。
我們可以在調用其他函數之前先調用isEmpty()函數判斷鏈表是否為空白,以避免對空鏈表進行錯誤操作。而對於以索引值index為參數的成員函數,我們還需要判斷該索引值是否位於有效範圍內。
我們可以利用Q_ASSERT()或者Q_ASSERT_X()加上一些合理的資訊來實現錯誤偵測。
參考文獻
http://www.cnblogs.com/mxly/archive/2010/10/21/1857372.html