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 .
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.
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)
Re: How to have the best of map and list ... is possible ?
i misunderstood your original question, sorry :o
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.
Re: How to have the best of map and list ... is possible ?
Ok, what I feared.
Thanks
Re: How to have the best of map and list ... is possible ?
Quote:
Originally Posted by
tonnot
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.