Swapping two values in QVector



  • 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!!!



  • 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.


  • Moderators

    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/



  • [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


  • Moderators

    [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()?



  • 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.


  • Lifetime Qt Champion

    [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


  • Moderators

    [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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.