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. Swapping two values in QVector
QtWS25 Last Chance

Swapping two values in QVector

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 10.9k Views
  • 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.
  • D Offline
    D Offline
    depecheSoul
    wrote on 23 Mar 2014, 08:21 last edited by
    #1

    Hello.

    I am trying to swap two values inside QVector. Here is how I tried to do it:
    @
    temp = storageArea->at(min_index);
    storageArea->insert(min_index, storageArea->at(f1));
    storageArea->insert(f1, temp);
    @
    My way of doing swaping does not work. When I run the program it frezzes, at the part where it should swap the value.

    Can you give me any idea how to swap two values inside QVecotor, or how to improve my code. Thanks!!!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bodzio131
      wrote on 23 Mar 2014, 08:47 last edited by
      #2

      First of all removing and inserting elements is not the best way to swap elements. Use [] to change particular element, not insert.
      Second thing is that you insert element to vector using reference to element from itself. As insert moves elements it's probable that you have dangling reference at some point.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 23 Mar 2014, 09:28 last edited by
        #3

        Hi,

        First of all, your code doesn't swap values. Instead, it tries to insert 2 new values into storageArea !

        Is there a reason why you are using a pocharer to a QVector? It's often best to allocate on the stack, not the heap:

        @
        // Do this:
        QVector<char> myVector1;

        // NOT this:
        QVector<char> *myVector2 = new QVector<char>;
        @

        Anyway, use std::swap() to swap elements:
        @
        QVector<char> myVector;
        myVector << 'a' << 'b' << 'c' << 'd';
        qDebug() << myVector;

        std::swap(myVector[0], myVector[3]);
        qDebug() << myVector;
        @

        Debug output:
        @
        QVector(a, b, c, d)
        QVector(d, b, c, a)
        @

        It is a good idea to learn the standard algorithm functions: http://www.cplusplus.com/reference/algorithm/

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • I Offline
          I Offline
          IamSumit
          wrote on 23 Mar 2014, 10:54 last edited by
          #4

          [quote author="depecheSoul" date="1395562895"]Hello.

          I am trying to swap two values inside QVector. Here is how I tried to do it:
          @
          temp = storageArea->at(min_index);
          storageArea->insert(min_index, storageArea->at(f1));
          storageArea->insert(f1, temp);
          @
          Can you give me any idea how to swap two values inside QVecotor, or how to improve my code. Thanks!!!
          [/quote]

          insert() of QVector will insert on specified place ;doesn't overwrite the value of QVector.
          Solution :

          QVector<int> vec;

          vec<<0<<1<<2<<3<<4;

          qDebug() << vec;

          int temp= vec.at(2);

          vec.replace(2,vec.at(1));

          vec.replace(1,temp);

          qDebug() << vec;

          Debug Output :

          QVector(0, 1, 2, 3, 4)

          QVector(0, 2, 1, 3, 4)

          Hope it helps

          Be Cute

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JKSH
            Moderators
            wrote on 23 Mar 2014, 12:10 last edited by
            #5

            [quote author="IamSumit" date="1395572069"]

            int temp= vec.at(2);

            vec.replace(2,vec.at(1));

            vec.replace(1,temp);

            [/quote]When would you use that instead of std::swap()?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • I Offline
              I Offline
              IamSumit
              wrote on 23 Mar 2014, 16:32 last edited by
              #6

              Obviously there is no comparison b/w these twos.

              [quote author="JKSH" date="1395576619"][quote author="IamSumit" date="1395572069"]

              int temp= vec.at(2);

              vec.replace(2,vec.at(1));

              vec.replace(1,temp);

              [/quote]When would you use that instead of std::swap()?

              [/quote]
              When you want to replace a value into QVector; (Qt Style).
              std::swap() also does some kind of replacement;It is good solution too in classic C++ style.

              Be Cute

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 23 Mar 2014, 21:54 last edited by
                #7

                [quote author="IamSumit" date="1395592323"]
                Obviously there is no comparison b/w these twos.

                [quote author="JKSH" date="1395576619"][quote author="IamSumit" date="1395572069"]

                int temp= vec.at(2);

                vec.replace(2,vec.at(1));

                vec.replace(1,temp);

                [/quote]When would you use that instead of std::swap()?

                [/quote]
                When you want to replace a value into QVector; (Qt Style).
                std::swap() also does some kind of replacement;It is good solution too in classic C++ style.
                [/quote]

                If you really want to do it Qt style, you have qSwap from QtAlgorithms.
                BUT these helper functions have been obsoleted and the use of the STL version is encouraged for new code

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

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 23 Mar 2014, 22:20 last edited by
                  #8

                  [quote author="SGaist" date="1395611670"]
                  [quote author="IamSumit" date="1395592323"]
                  Obviously there is no comparison b/w these twos.

                  [quote author="JKSH" date="1395576619"][quote author="IamSumit" date="1395572069"]

                  int temp= vec.at(2);

                  vec.replace(2,vec.at(1));

                  vec.replace(1,temp);

                  [/quote]When would you use that instead of std::swap()?

                  [/quote]
                  When you want to replace a value into QVector; (Qt Style).
                  std::swap() also does some kind of replacement;It is good solution too in classic C++ style.
                  [/quote]

                  If you really want to do it Qt style, you have qSwap from QtAlgorithms.
                  BUT these helper functions have been obsoleted and the use of the STL version is encouraged for new code[/quote]std::swap() IS Qt-style :)

                  Regarding the C++ language, Qt aims to:

                  Provide features that the C++ standard doesn't, and

                  Make C++ easier to use.

                  Since the standard algorithm functions work very well and are already easy to use, Qt recommends using them instead of custom q-functions.

                  Internally, Qt engineers even replaced "old Qt-style" functions with the standard ones recently, e.g. https://codereview.qt-project.org/#patch,all,75604,1

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    IamSumit
                    wrote on 24 Mar 2014, 07:25 last edited by
                    #9

                    Thanks both of you for Updating my knowledge
                    Your reply(s) are very informative.
                    :)

                    Be Cute

                    1 Reply Last reply
                    0

                    7/9

                    23 Mar 2014, 21:54

                    • Login

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