Results 1 to 10 of 10

Thread: How to modify qmap without iteration

  1. #1
    Join Date
    Apr 2010
    Location
    Izmir, Turkey
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to modify qmap without iteration

    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.

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    Qt Code:
    1. if(!map.contains(key))
    2. map.insert(key, defaultValue);
    3. else
    4. map[key]++;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2010
    Location
    Izmir, Turkey
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    Quote Originally Posted by spud View Post
    Qt Code:
    1. if(!map.contains(key))
    2. map.insert(key, defaultValue);
    3. else
    4. map[key]++;
    To copy to clipboard, switch view to plain text mode 
    yes, it works, but I tried this

    Qt Code:
    1. QMap<QString, int > *temp = new QMap<QString, int >;
    2. temp->insert("first", 1);
    3. 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.

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    This is an ugly C++ pitfall. Since temp is a pointer, you would have to write:
    Qt Code:
    1. (*temp)["first"] = 2;
    To copy to clipboard, switch view to plain text mode 
    but an altogether better approach would be:
    Qt Code:
    1. QMap<QString, int > temp;
    2. temp.insert("first", 1);
    3. temp["first"] = 2;
    To copy to clipboard, switch view to plain text mode 
    See implicit sharing.

  5. The following user says thank you to spud for this useful post:

    sanjarbek (12th April 2010)

  6. #5
    Join Date
    Apr 2010
    Location
    Izmir, Turkey
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    Thank you, it is working, but it seems some ugly.

    I need to create QMap variable dynamically.

  7. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    I need to create QMap variable dynamically.
    Dynamically, sure. On the heap, probably not.
    Have you read about implicit sharing?

  8. #7
    Join Date
    Apr 2010
    Location
    Izmir, Turkey
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    Quote Originally Posted by spud View Post
    Dynamically, sure. On the heap, probably not.
    Have you read about implicit sharing?
    Yes, I have read it before.
    In my program I have a class which have 3 members: bool, QMap, int.
    In the case of bool variable I need either QMap or int.
    This is why I want to create variables dynamically.

  9. #8
    Join Date
    Apr 2010
    Location
    Izmir, Turkey
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    I changed my code.

    Qt Code:
    1. QMultiMap<QString, QMap<QString, int> > mTable;
    2. if (mTable.value(instancevalue).contains(classname))
    3. mTable[instancevalue][classname]++;
    4. else
    5. mTable[instancevalue].insert(classname, 1);
    To copy to clipboard, switch view to plain text mode 

    get this error message

    Qt Code:
    1. 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 private
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    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

  11. #10
    Join Date
    Apr 2010
    Location
    Izmir, Turkey
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to modify qmap without iteration

    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.

Similar Threads

  1. how to modify contents of QScrollArea?
    By cyrfer in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2010, 09:28
  2. How to modify QHash values?
    By darshan in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2009, 12:47
  3. Modify a ContextMenu
    By smarinr in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2008, 17:41
  4. Iteration problem
    By Pharell in forum General Programming
    Replies: 1
    Last Post: 1st October 2007, 10:56
  5. GLSL shaders - keeping a value per fragment iteration
    By high_flyer in forum General Programming
    Replies: 3
    Last Post: 22nd June 2007, 15:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.