简体   繁体   English

如何在 Qt 6 中将 QList 转换为 QSet

[英]How to convert QList to QSet in Qt 6

I am porting my application to Qt 6 and as I am reading documentation I see that classes are cleaned up a lot, QList and QVector are unified, QStringList is now alias to QList<QString> and so on.我正在将我的应用程序移植到 Qt 6 并且当我阅读文档时,我看到类被清理了很多, QListQVector是统一的, QStringList现在是QList<QString>的别名等等。

But now this gave me one problem.但是现在这给了我一个问题。

In my code (which is Qt 5) I am converting QStringList to QSet to eliminate duplicates from the list.在我的代码(即 Qt 5)中,我将QStringList转换为QSet以消除列表中的重复项。 I went through new documentation but I haven't seen a way to convert QList to QSet in Qt 6.我浏览了新文档,但我还没有看到在 Qt 6 中将QList转换为QSet的方法。

So how do you convert QList to QSet ?那么如何将QList转换为QSet Or it is not possible and I will need to write a helper function for removing duplicates?或者这是不可能的,我需要编写一个帮助程序 function 来删除重复项?

EDIT: I am using Qt 6.0.1.编辑:我正在使用 Qt 6.0.1。

I must admit that I'm still on Qt5.我必须承认我还在 Qt5 上。

However, QList and QSet remind me strong to std::list and std::set .但是, QListQSet提醒我强烈std::liststd::set For the latter, there was already another (and more flexible) way to achieve such things: using construction with iterators.对于后者,已经有另一种(并且更灵活)的方式来实现这些目标:使用带有迭代器的构造。 A short check in the Qt6 doc. Qt6 文档中的简短检查。 convinced me, this should work in the Qt classes as well:说服我,这也应该在 Qt 类中工作:

QSet::QSet(InputIterator first, InputIterator last)QSet::QSet(InputIterator first, InputIterator last)

Constructs a set with the contents in the iterator range [first, last).使用迭代器范围 [first, last) 中的内容构造一个集合。

The value type of InputIterator must be convertible to T. InputIterator 的值类型必须可转换为 T。

Note: If the range [first, last) contains duplicate elements, the first one is retained.注意:如果范围 [first, last) 包含重复元素,则保留第一个元素。

This function was introduced in Qt 5.14.此 function 在 Qt 5.14 中引入。

where first is set with first设置为

QList::iterator QList::begin() QList::迭代器 QList::begin()

Returns an STL-style iterator pointing to the first item in the list.返回指向列表中第一项的 STL 样式迭代器。

and last with last

QList::iterator QList::end() QList::迭代器 QList::end()

Returns an STL-style iterator pointing to the imaginary item after the last item in the list.返回一个 STL 风格的迭代器,指向列表中最后一项之后的虚构项。

How this should look like when put together:放在一起应该是什么样子:

QList<QString> aList;
// populate list
QSet<QString> aSet(aList.begin(), aList.end());

OP noted that the Qt-5 doc. OP 注意到 Qt-5 文档。 already contained a hint concerning this:已经包含有关此的提示:

QSet QList::toSet() const QSet QList::toSet() 常量

Note: Since Qt 5.14, range constructors are available for Qt's generic container classes and should be used in place of this method.注意:自 Qt 5.14 起,范围构造函数可用于 Qt 的通用容器类,应使用此方法代替。

OP also mentioned a convenience wrapper: OP 还提到了一个方便的包装器:

template <typename T>
QSet<T> QListToQSet(const QList<T>& qlist)
{
  return QSet<T> (qlist.constBegin(), qlist.constEnd());
}

applied to the above sample:应用于上述样本:

QList<QString> aList;
// populate list
QSet<QString> aSet = QListToQSet(aList);

A more general conversion facility would probably be something like this:更通用的转换工具可能是这样的:

template <typename T, template<typename> typename C>
QSet<T> toQSet(const C<T> &container)
{
  return QSet<T>(container.begin(), container.end());
}

which would work even with matching std containers as well:即使与匹配的std容器也可以使用:

std::vector<QString> aVec = { "Hello", "World", "Hello" };
QSet<QString> aSet = toSet(aVec);

you are right, see in the code or the doc that QStringList is a child of QList你是对的,在代码文档中看到 QStringList 是 QList 的孩子

class QStringList : public QList<QString>
{

so you can call QSet toSet() const;所以你可以调用 QSet toSet() const; directly since the method is public.直接因为方法是公开的。

with this, you can unzip directly the content of the QStringlist to a qset有了这个,您可以直接将QStringlist的内容解压缩到qset

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString str = "one,two,three,four,one";
    QStringList strlt = str.split(QChar(','));

    const QSet<QString> strVe = strlt.toSet();
    qDebug() << strVe;

    return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM