How to modify QMap without iteration,
In my code I verify qmap if it has a key.
If no then I insert a new key and value. If yes then I want to increment a value.
How to do this?
Sorry for my English.
How to modify QMap without iteration,
In my code I verify qmap if it has a key.
If no then I insert a new key and value. If yes then I want to increment a value.
How to do this?
Sorry for my English.
Qt Code:
if(!map.contains(key)) map.insert(key, defaultValue); else map[key]++;To copy to clipboard, switch view to plain text mode
yes, it works, but I tried this
Qt Code:
QMap<QString, int > *temp = new QMap<QString, int >; temp->insert("first", 1); temp["first"] = 2;To copy to clipboard, switch view to plain text mode
and get this error
C:/QtProjects/NaiveBayesian/attribute.cpp:42: error: invalid types 'QMap<QString, int>*[const char [6]]' for array subscript
Last edited by sanjarbek; 12th April 2010 at 18:02.
This is an ugly C++ pitfall. Since temp is a pointer, you would have to write:
but an altogether better approach would be:
See implicit sharing.Qt Code:
QMap<QString, int > temp; temp.insert("first", 1); temp["first"] = 2;To copy to clipboard, switch view to plain text mode
sanjarbek (12th April 2010)
Thank you, it is working, but it seems some ugly.
I need to create QMap variable dynamically.
Dynamically, sure. On the heap, probably not.I need to create QMap variable dynamically.
Have you read about implicit sharing?
I changed my code.
Qt Code:
QMultiMap<QString, QMap<QString, int> > mTable; if (mTable.value(instancevalue).contains(classname)) mTable[instancevalue][classname]++; else mTable[instancevalue].insert(classname, 1);To copy to clipboard, switch view to plain text mode
get this error message
Qt Code:
C:/QtProjects/NaiveBayesian/../../Qt/2010.02.1/qt/include/QtCore/../../src/corelib/tools/qmap.h:1013: error: 'T& QMultiMap<Key, T>::operator[](const Key&) [with Key = QString, T = QMap<QString, int>]' is privateTo copy to clipboard, switch view to plain text mode
See the documentation of QMultiMap:
Unlike QMap, QMultiMap provides no operator[]. Use value() or replace() if you want to access the most recently inserted item with a certain key
I understood. Thank you for your help. Until now I used dotnet and now I pass to qt. My C++ background is good, just I get accustomed dotnet style model.
Bookmarks