0

I have a dynamic array.

int* array = new int[size]

I am using the Qt library to develop an application in C++11.

Can I use qSort from QtAlgorithms.h on it this way:

qSort(array, array+size-1)

I am not sure since the arguments require a type of RandomAccessIterator

Does that imply any pointer ?

Jonas
  • 6,915
  • 8
  • 35
  • 53
marc
  • 797
  • 1
  • 9
  • 26
  • 3
    If you're using `c++11` then `std::sort(array, array + size)` is probably a better option. – G.M. Jun 20 '17 at 19:21
  • 1
    `qSort` is stated as obsolete. also the end of range means actually out of range for both `qSort` and `std::sort` so `array+size` is the proper index for the end. – Alexander V Jun 20 '17 at 19:32
  • @AlexanderVX what do you mean obsolete ? I am referring to the qSort that is part of the Qt library – marc Jun 20 '17 at 19:33
  • Qt company apparently maintains some algorithms only for the backward compatibility: http://doc.qt.io/qt-5/qtalgorithms-obsolete.html#qSort They imply: use STL for that. – Alexander V Jun 20 '17 at 19:34
  • ok thanks guys. Should be a simple swap. I can just call `std::sort` instead – marc Jun 20 '17 at 20:02
  • @marc Consider creating an answer yourself. It may be useful for others with the same question. – m7913d Jun 20 '17 at 20:21
  • Use std::vector instead of new[]. –  Jun 20 '17 at 22:45

1 Answers1

0

qSort() has been deprecated since Qt 5.2. Quoting from the ChangeLog:

With STL no longer being optional for building and using Qt, a number of parts of QtAlgorithms no longer make sense, and have therefore been deprecated.

Replacements are available in the STL, and generally have much better performance, but are not instantly source-compatible in all cases. For instance, specialization of qLess or qSwap means that a direct port to std::sort from qSort may not be possible, as std::sort does not use any of them; a possible workaround is... [see link for more info].

Note that as the ChangeLog mentions, there are cases where a trivial find/replace is not sufficient.

For some discussion on the differences in speed of std::sort() and qSort(), see here.

These are just warnings now, but will likely be errors with Qt 6.

scottkosty
  • 2,410
  • 1
  • 16
  • 22