Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Put QList items inside a QMap



  • I was wondering if it's possible to add a QList<QString> values inside of a QMap<QString, int> without the need to loop over QList. Is there a Qt or C++ std API for that? All the ints will be false.



  • @Mr-Gisa
    (I'm sure you can save a tiny bit of code via a std:: method.) But does it really matter? Isn't the time taken to loop through the list trivial, at least compared to creating the items in the map, which ultimately I presume must be done one at a time?



  • Actually it doesn't matter, I'm just curious, I was wondering and decided to ask.

        for (const auto &item : qAsConst(items)) {
            mItems.insert(item, false);
        }
    

    That is how I'm doing it.



  • @Mr-Gisa
    That seems fine! Ultimately there's not going to be a "create many map keys from many input keys all in one go" low-level functionality, so you will still have to iterate through the inputs.

    What you could do is if your input list is sorted by key already, look at iterator QMap::insert(const_iterator pos, const Key &key, const T &value) to save insertion look-up time. However, if your input list is not already in sorted order, it won't be worth sorting it first! :)

    There's also QMap::QMap(std::initializer_list<std::pair<Key, T> > list), but I don't think you start with a suitable initializer list format....



  • @Mr-Gisa You may consider using only one QMap, except if you need to keep insertion order of your QList. Then you can access the list when needed using QMap::keys().



  • @Mr-Gisa said in Put QList items inside a QMap:

    All the ints will be false.

    BTW, if this is always the case, you don't need a QMap and a QHash will be quicker/smaller.



  • @Gojir4 said in Put QList items inside a QMap:

    QMap

    What you mean with using only one QMap? I'm using only one QMap already.

    @JonB

    BTW, if this is always the case, you don't need a QMap and a QHash will be quicker/smaller.

    Yes, it's a QMap<QString, bool>. In this case is better to use a QHash? Why?



  • @Mr-Gisa What I mean is to replace the QList with QMap (or QHash)



  • The QList is an argument of a method, I don't have two containers in the class.



  • @Mr-Gisa said in Put QList items inside a QMap:

    Yes, it's a QMap<QString, bool>. In this case is better to use a QHash? Why?

    From the doc of QMap:

    QMap and QHash provide very similar functionality. The differences are:

    • QHash provides average faster lookups than QMap. (See Algorithmic Complexity for details.)

    • When iterating over a QHash, the items are arbitrarily ordered. With QMap, the items are always sorted by key.

    • The key type of a QHash must provide operator==() and a global qHash(Key) function. The key type of a QMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.



  • Thank you guys, helped a lot.



  • @Mr-Gisa
    I was actually going to say:

    If you have nothing useful to store for the "value" part of a QHash/QMap, your case could actually suffice with a QSet. The advantage being (given that QSet is only really hiding a QHash anyway), there is a QSet<T> QSet::fromList(const QList<T> &list) (http://doc.qt.io/qt-5/qset.html#fromList) static method which would satisfy for sure your adding of all the strings in your list without you having to write any iteration code/function :)


Log in to reply