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 get true or false in a single line code on QVector?
Forum Updated to NodeBB v4.3 + New Features

how to get true or false in a single line code on QVector?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 862 Views 1 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.
  • T Offline
    T Offline
    thippu
    wrote on last edited by
    #1

    Hi,

    1. I have typedef QPair<QString,QString> group_key;
    2. I have global QVector<group_key>checkboxItems;
    3. I did populate QVector with the QPairobjects
    4. In a method, I would like to check an element the QPair exists on QVector, Is there any way to check in a line of code?
      code:
    void someMethod()
    {
     bool checkbox_item_there=QVector.anymethod("string1","string2")// using str1, str2 can we check, Is there any method there?
    }
    
    jsulmJ ODБOïO 2 Replies Last reply
    0
    • T thippu

      Hi,

      1. I have typedef QPair<QString,QString> group_key;
      2. I have global QVector<group_key>checkboxItems;
      3. I did populate QVector with the QPairobjects
      4. In a method, I would like to check an element the QPair exists on QVector, Is there any way to check in a line of code?
        code:
      void someMethod()
      {
       bool checkbox_item_there=QVector.anymethod("string1","string2")// using str1, str2 can we check, Is there any method there?
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @thippu http://doc.qt.io/qt-5/qvector.html#contains

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      6
      • T thippu

        Hi,

        1. I have typedef QPair<QString,QString> group_key;
        2. I have global QVector<group_key>checkboxItems;
        3. I did populate QVector with the QPairobjects
        4. In a method, I would like to check an element the QPair exists on QVector, Is there any way to check in a line of code?
          code:
        void someMethod()
        {
         bool checkbox_item_there=QVector.anymethod("string1","string2")// using str1, str2 can we check, Is there any method there?
        }
        
        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        hi @thippu

        from the doc : http://doc.qt.io/qt-5/qvector.html
        int i = vector.indexOf("str1");
        if (i != -1)
        cout << "First occurrence of str1 is at position " << i << endl;

        not sure this is fully what you need

        T 1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by
          #4

          I'd go with a lambda and std::any_of:

          any_of( checkBoyItems.cbegin(), checkBoyItems.cend(),
              [&str1, &str2]( const QPair< QString, QString >& item )
              {
                  return item.first == str1 && item.second == str2;
              );
          

          But, honestly, why the one line constraint?

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          T 1 Reply Last reply
          4
          • jsulmJ jsulm

            @thippu http://doc.qt.io/qt-5/qvector.html#contains

            T Offline
            T Offline
            thippu
            wrote on last edited by
            #5

            @jsulm Thank you.

            1 Reply Last reply
            0
            • JohanSoloJ JohanSolo

              I'd go with a lambda and std::any_of:

              any_of( checkBoyItems.cbegin(), checkBoyItems.cend(),
                  [&str1, &str2]( const QPair< QString, QString >& item )
                  {
                      return item.first == str1 && item.second == str2;
                  );
              

              But, honestly, why the one line constraint?

              T Offline
              T Offline
              thippu
              wrote on last edited by thippu
              #6

              @JohanSolo

              But, honestly, why the one line constraint?

              Because I would not want the loop to consume more time and I did not know this method.
              Thank you all

              J.HilkJ 1 Reply Last reply
              0
              • ODБOïO ODБOï

                hi @thippu

                from the doc : http://doc.qt.io/qt-5/qvector.html
                int i = vector.indexOf("str1");
                if (i != -1)
                cout << "First occurrence of str1 is at position " << i << endl;

                not sure this is fully what you need

                T Offline
                T Offline
                thippu
                wrote on last edited by
                #7

                @LeLev Thanks.

                1 Reply Last reply
                0
                • T thippu

                  @JohanSolo

                  But, honestly, why the one line constraint?

                  Because I would not want the loop to consume more time and I did not know this method.
                  Thank you all

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @thippu said in how to get true or false in a single line code on QVector?:

                  @JohanSolo

                  But, honestly, why the one line constraint?

                  Because I would not want the loop to consume more time and I did not know this method.
                  Thank you all

                  Lines of code are pretty meaningless in relation to app performance: Depending on the situation, writing more lines can in fact improve the over all speed. That solely depends on the compiler.

                  In fact, as soon as you build your progam in release mode, the code you've written looses virtually all meaning. Debug is a different case entirely.


                  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
                  0

                  • Login

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