文章目錄
Detailed Description
The QCache class is a template class that provides a cache.
QCache defines a cache that stores objects of type T associated with keys of type Key. For example, here’s the definition of a cache that stores objects of type Employee associated with an integer key:
以下是QCache的簡單使用程式:
#include <qapplication.h>
#include <qwidget.h>
#include <qmultilineedit.h>
#include <qcache.h>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QWidget w;
w.resize(150,150);
a.setMainWidget(&w);
QMultiLineEdit edit(&w);
edit.setGeometry(10,10,130,130);
typedef QCache<char> StringCache;
StringCache stringcache;
stringcache.setMaxCost(6);
stringcache.insert("Sweden","Stockholm",2);
stringcache.insert("Germany","Berlin",2);
stringcache.insert("France","Paris",2);
stringcache.insert("England","London",2);
edit.insertLine(stringcache["England"]);
edit.insertLine(stringcache["France"]);
edit.insertLine(stringcache["Cermany"]);
edit.insertLine(stringcache["Sweden"]);
w.show();
a.exec();
}
QCache 能用來建立具有大小受限的散列表。
stringcache.setMaxCost(6) 為這個散列表設定了最大成本
stringcache.insert(“Sweden”,”Stockholm”,2); 這表示每一個元素的成本為2。所以這樣一共只能插入3個元素了。。。。。。當插入元素超過3個時,這個緩衝會自動刪除最長時間沒有被訪問的元素,又因為所有的元素都未被訪問,故刪除第一個插入的元素。