Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to compare QStringList to std::string vector
Forum Updated to NodeBB v4.3 + New Features

how to compare QStringList to std::string vector

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 6 Posters 9.3k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on 16 Sept 2018, 10:55 last edited by
    #1

    how to compare a std::vector<string> to a QStringList

    K 1 Reply Last reply 16 Sept 2018, 11:12
    0
    • Q Qt Enthusiast
      16 Sept 2018, 10:55

      how to compare a std::vector<string> to a QStringList

      K Offline
      K Offline
      koahnig
      wrote on 16 Sept 2018, 11:12 last edited by
      #2

      @Qt-Enthusiast

      Checkout the different conversion routines for QList and QVector There are a couple of options for conversions.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • F Offline
        F Offline
        Flotisable
        wrote on 16 Sept 2018, 11:19 last edited by
        #3

        Though you can convert the QStringList to std::vector<QString> by using QStringList::toVector() and QVector::toStdVector.
        But there seems to be no single function to compare a std::string to a QString.
        So you need to write one such function.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Sept 2018, 21:11 last edited by
          #4

          Hi,

          What exact comparison do you have in mind ? That they have the same content in the same order ? Or that they have all values in common ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply 17 Sept 2018, 06:39
          0
          • S SGaist
            16 Sept 2018, 21:11

            Hi,

            What exact comparison do you have in mind ? That they have the same content in the same order ? Or that they have all values in common ?

            Q Offline
            Q Offline
            Qt Enthusiast
            wrote on 17 Sept 2018, 06:39 last edited by
            #5

            @SGaist

            alll values in common

            A 1 Reply Last reply 17 Sept 2018, 06:45
            0
            • Q Qt Enthusiast
              17 Sept 2018, 06:39

              @SGaist

              alll values in common

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 17 Sept 2018, 06:45 last edited by
              #6

              @Qt-Enthusiast

              You cannot directly compare QString and std::string as the former is Unicode UTF-16 and the latter may have one of the 8 bit encodings.

              So you need to convert one of them anyway.

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 17 Sept 2018, 06:59 last edited by
                #7

                from what the others already said, you could do something like this:

                bool compareQStdString(const QString &str1, const std::string &str2){
                    return (str1 == QString::fromStdString(str2));
                }
                
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    QList<QString> qstrings({"a","b","c","d","e","f","g"});
                
                    std::vector<std::string>strings({"a","b","b","d","d","f","f"});
                
                    for(int i(0); i < qstrings.size() && i < strings.size(); i++){
                        qDebug() << "Strings are at index"<< i << "are equal:" << compareQStdString(qstrings.at(i), strings.at(i));
                    }
                
                    return a.exec();
                }
                
                /*Result:
                Strings are at index 0 are equal: true
                Strings are at index 1 are equal: true
                Strings are at index 2 are equal: false
                Strings are at index 3 are equal: true
                Strings are at index 4 are equal: false
                Strings are at index 5 are equal: true
                Strings are at index 6 are equal: false
                */
                

                It's probably not the fastest or most elegant way to do it, but it works at least.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2

                1/7

                16 Sept 2018, 10:55

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved