0

I have a QMAP data structure. Now, I want to insert the QVariant type in the QMAP. The data is inserted based on the priority. For example priority 1, 2.. etc. These priority is the key in the QMAP. However, I can have the same key values - meaning same priority. This means priority 1, and 1 can have different QVariants. In order to suffice this, I am using insertMulti rather than insert. Now, the difficulty is that the last insertMulti having the same key is getting inserted on the top of the previously insert value. Now, how can I make it reverse?

QMAP<int, QVariant> grp;

grp.insertMulti(0, "HELLO");
grp.insetMulti(0. "Hi");

On reading the values -

It first returs Hi. However, I want it to return HeLLO. How can I do so? Please don't give answers in using other data structures. This is a snippet of a very complex problem.

dexterous
  • 6,422
  • 12
  • 51
  • 99
  • How are you reading the values? Note that there's an overloaded insertMulti function, allowing you to hint at the position that the item is inserted: http://qt-project.org/doc/qt-5/qmap.html#insertMulti-2 – TheDarkKnight Sep 23 '14 at 09:08
  • I am using .value() to read the values back. – dexterous Sep 23 '14 at 09:11
  • .value() returns just one item. Do you mean .values(const Key & key), or are you iterating through the class? Perhaps you can add this code to your question. – TheDarkKnight Sep 23 '14 at 09:13

4 Answers4

1

How the values are stored internally in the map is not the problem, but rather how to retrieve them in the required 'priority' order.

As you've stated that the "data is inserted based on the priority" and you want the values retrieved in the same order, you can use the QMap::values(const Key & key) const function for which the docs state:-

Returns a list containing all the values associated with key key, from the most recently inserted to the least recently inserted one.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Yeah, but I want just opposite to it to be stored. – dexterous Sep 23 '14 at 09:35
  • 1
    Why do you care how it's stored? You're inserting them in a particular order and retrieving them that way. If you want the reverse, you can use the values(const Key&) function and iterate through it backwards, from the end() iterator to begin(). Alternatively, add them in the reverse order, or am I misunderstanding your requirements? – TheDarkKnight Sep 23 '14 at 09:36
0

I would do it in the following way:

QMap<int, QVariant> grp;

grp.insertMulti(0, "HELLO");
grp.insertMulti(0, "Hi");

QList<QVariant> vl = grp.values(0);
QString firstInserted = vl.last().toString(); // Returns the 'HELLO'.
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • I don't want to change the reading method. I want to change only the storing procedure. – dexterous Sep 23 '14 at 09:35
  • @dexterous_stranger, than implement you own container class. – vahancho Sep 23 '14 at 09:46
  • One idea comes to my mind is to iterate and copy the map again. Now, the mostly recent used will get at the top. So, basically the same key one's will get changed now. – dexterous Sep 23 '14 at 09:52
0

Problem - If the multiple keys are stored in the same key, then the last value added for the same key will have a preference. As per the document - Returns a list containing all the values associated with key key, from the most recently inserted to the least recently inserted one.

Solution - To get it back to the same form - again traverse the QMAP and insert it in the other QMAP. Now, the values associated with the same key will be reversed again - based on the fundamental that recently accessed will be at the top for the same key value.

QMAP<int, QVariant> grp;

grp.insertMulti(0, "HELLO");
grp.insetMulti(0. "Hi");

solution

QMAP<int, QVariant> pgrp;

QMap<int, QVariant>::const_iterator pGroupServiceIterator = grp.constBegin();

while (pGroupServiceIterator != grp.constEnd())
            {
                   pgrp.insertMulti(pGroupServiceIterator.key(),pGroupServiceIterator.value());

               ++pGroupServiceIterator;
            }

Now, the print you will get is "HELLO" first rather than Hi when the pgrp is traversed and printed.

dexterous
  • 6,422
  • 12
  • 51
  • 99
0
QMultiMap<int, QVariant> grp;
grp.insertMulti(0, "HELLO");
QMultiMap<int, QVariant> map {{ 0, "HI" }};
grp = map.unite(grp);

QList<QVariant> vl = grp.values(0);
QString firstInserted = vl.last().toString();
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Nov 15 '21 at 19:25
  • I was trying to solve the exact same issue and was not able to find a solution. This is what worked for me. When a key collision happens, the unite will put grp before the newer map entry. Putting this code in a method is of course the way to implement this but the above code is for simple illustration. – jrbloch Nov 16 '21 at 22:22