qmap-example.cpp

来自「压缩包里有教材<<C++模式设计-基于QT4开源跨平台开发框架&gt」· C++ 代码 · 共 51 行

CPP
51
字号
#include "textbook.h"#include <QMap>#include <qstd.h>using namespace qstd;//start id=implTextbookMap::~TextbookMap() {    cout << "Destroying TextbookMap ..." << endl;    foreach (QString key, keys())    /* keys() is a QMap function. */        delete value(key);    /* Get and delete a pointer from the map. */     clear(); }void TextbookMap::add(Textbook* text) {    insert(text->getIsbn(), text);}void TextbookMap::showAll() const {    foreach (QString key, keys()) {        Textbook* tb = value(key);          cout << '[' << key << ']' << ":"              << tb->toString() << endl;    }}//end//start id=clientint main() {    Textbook* t1 = new Textbook("The C++ Programming Language",        "Stroustrup", "0201700735", 1997);    Textbook* t2 = new Textbook("XML in a Nutshell",         "Harold","0596002920", 2002);    Textbook* t3 = new Textbook("UML Distilled",         "Fowler", "0321193687", 2004);    Textbook* t4 = new Textbook("Design Patterns", "Gamma",        "0201633612",1995);    { /* Inner block for demonstration purposes */        TextbookMap m;        m.add(t1);        m.add(t2);        m.add(t3);        m.add(t4);        m.showAll();        m.remove (t3->getIsbn());  /* Removed but not deleted */    }  /* End of block - local variables destroyed. */    cout << "After m has been destroyed we still have: \n"             << t3->toString() << endl;    return 0;}//end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?