smyslov smyslov - 4 months ago 18
C++ Question

Find duplicates in vector using qt

I have a csv file whose contents look like the following:

Source Target LinkId LinkName Throughput
==================================================
1 12 1250 link1250 5
1 12 3250 link3250 14
1 14 1250 link1250 5
1 14 3250 link3250 14
1 18 1250 link1250 5
1 18 3250 link3250 14
2 12 2250 link2250 5
2 12 5250 link5250 14
2 14 2250 link2250 5
2 14 5250 link5250 14
2 18 2250 link2250 5
2 18 5250 link5250 14


and so on.
The goal is to find the number of links that are multicast from each source node, i.e., for source 1, the targets are 12,14,18 for linkID 1250, for linkID 3250, for source 1, the targets are 12,14,18 and so on.

I have been using Qt and have read the csv into a vector of structs as follows:

struct edgeDetails_t{
int source;
int target;
int linkID;
QString linkName;
int throughput;
};
QVector<edgeDetails_t> multiCastLinks;


In order to achieve the goal as stated above, I have tried to use
QHash


QHash<int, QList<int>> multiCastSenders;


with source as the key but I am not sure if this is the right way of doing this.
Could someone please let me know any other way of doing this.

Answer

This is also possible with QMultiMap which allows multiple keys, then you can use method keys() to get the keys and values(key) to get respective values.

To get rid of duplicate values you can use toSet() which returns only unique values of respective list.

QMultiMap<int, int> test;
test.insert(1, 12);
test.insert(1, 12);
test.insert(1, 12);
test.insert(1, 14);
test.insert(1, 18);
test.insert(1, 18);

test.insert(2, 12);
test.insert(2, 12);
test.insert(2, 12);
test.insert(2, 14);
test.insert(2, 18);
test.insert(2, 18);
qDebug() << "size:" << test.size();
QSet<int>::iterator it;
QSet<int> keys = test.keys().toSet();
qDebug() << "keys:" << keys;
for(it = keys.begin(); it != keys.end(); ++it) {
    qDebug() << "key:" << *it << "value:" << test.values(*it).toSet();
}

Output:

size: 12
keys: QSet(1, 2)
key: 1 value: QSet(12, 14, 18)
key: 2 value: QSet(12, 14, 18)

You can figure out the rest with your cool struct.

HTH