Results 1 to 6 of 6

Thread: How to have the best of map and list ... is possible ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default How to have the best of map and list ... is possible ?

    I have the next problem :
    I want to store data from a grid. Some cells has values, others not.
    Ok, MAP can let me to do it. If I have values for cells 1 and 100 I have only two elements easily stored. But I cannot insert without looping around rows and cols to copy offset and open a new empty space (that is to say: add 101, 101 = 100, new data for 100)
    Ok, List, however has the insert method. So I have not to loop when insert. But if I have values for cells 1 and 100 I need to store 100 elements...
    So any easy idea to use a special container that let me to do what I want
    (a key-value data and fast insert method ?)
    Thanks .

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: How to have the best of map and list ... is possible ?

    QMap let you insert elements (and it's more efficient than QList) see the operator[] or insert //std::map also has this functionality

    You can read more about Qt containers here, algorithmic complexity of operations is presented there too.

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to have the best of map and list ... is possible ?

    Thanks Zlat.
    I want to use Map becaus it does not store any data between index1 and index7 (for example) ,
    But I cannot do this :
    I have :
    map [1]=3331
    map [2]=2121
    map [3]=82121

    How can I insert data to have :

    map [1]=3331
    map [3]=2121
    map [4]=82121

    (Of course it is an example, I can have a million of elements instead of 4)

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: How to have the best of map and list ... is possible ?

    i misunderstood your original question, sorry

    For the map data structure that is not an actual "index", even if if looks so (because of the operator[] ) a map stores a key, value pair not an indexed array of values.
    So if you need to use the key as an index map is not a good choice, map is a good choice when key and value are more closely related.

    Anyway, you still can do what you want with a map and save some memory (you can manually copy the elements in an loop and then insert), but for millions of elements the copy might be noticeable.

    So you will need to decide what data structure to use and test to see which solution performs better against your worse data insertions.

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to have the best of map and list ... is possible ?

    Ok, what I feared.
    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to have the best of map and list ... is possible ?

    Quote Originally Posted by tonnot View Post
    I want to use Map becaus it does not store any data between index1 and index7 (for example)
    Use a list of maps then and have an additional structure (possibly a list) to map keys to indexes of the the list if you really need to.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2011, 18:33
  2. List View with sections for alphabetialy sorted list
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2010, 12:50
  3. Qt list help
    By shabir_24 in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2010, 17:07
  4. rss list
    By tyzhang3 in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2008, 00:12
  5. list of list
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 3rd July 2006, 09:11

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
  •  
Qt is a trademark of The Qt Company.