Qt 容器類 QCache使用

來源:互聯網
上載者:User
文章目錄
  • Detailed Description
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個時,這個緩衝會自動刪除最長時間沒有被訪問的元素,又因為所有的元素都未被訪問,故刪除第一個插入的元素。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.