Solved sortable dual item data type
-
I am trying to figure out the best data type for a video duplicate finder I am writing. I have a qlist the holds a struct of this type
struct frm { QString file; int position; cv::Mat descriptors; };
I want to add to it a list of an item that contains a string for the video that it matches with and a vector? or something sortable that will hold the matching frame pairs (in milliseconds)
I need to be able to easily check for the video in this type so that I don't make duplicates and only add matching frames as they are detected. the matching pair of frames needs to be sortable since the matches will be found out of order possibly (qtconcurrent).
Since the frame matches themselves won't change, they can possibly be just a string separated by a space like "1500 1500" . The only problem with that is that I'm not sure if it will sort correctly as I need it sorted by the first value only.
So what type or types would you recommend for something like this?
-
Hi! You can easily create your own class that is sortable by
qSort()
. You just need to implement its<
operator. The following code shows a class MatchItem that stores two int values and implements a<
operator that looks only at the first of these two values for the comparison:class MatchItem { public: MatchItem(int a=0, int b=0) : m_a(a) , m_b(b) {} int a() const { return m_a; } int b() const { return m_b; } bool operator<(const MatchItem &rhs) const { return m_a < rhs.a(); } private: int m_a; int m_b; };
The following is only for debugging purposes:
QDebug operator<< (QDebug d, const MatchItem &item) { d << QString("%1, %2").arg(item.a()).arg(item.b()); return d; }
And here is how to sort a list of MatchItems with qSort:
#include <QList> #include <QtAlgorithms> #include <QDebug> // ... QList<MatchItem> list; list << MatchItem(1,2) << MatchItem(3,7) << MatchItem(-3,6) << MatchItem(2, 8); qSort(list.begin(), list.end()); qDebug() << list;
-
This post is deleted!