[Interest] qsort() Obsolete?

André Somers andre at familiesomers.nl
Fri Apr 17 12:22:28 CEST 2015


Berkay Elbir schreef op 17-4-2015 om 12:12:
> Hello All,
>
> I want to ask a question to you to be certain. Is void qsort() 
> function obsolete? Should we use std::sort instead of this function? 
> Because I have a priority list and need to sort it.
>
Yes, in general the things you can find in <QtAlgorithms> should be 
replaced with their stl (or other) counterparts. If you are just like I 
am and do not like the verbose syntax of specifying two iterators 
instead of just the whole container, you can use a small macro (or 
specify your own overloads for all algorithms of course). I use:

/**

   *  shorthand  to  writing  a  range  with  a  c.constBegin  and  c.constEnd.  Instead,  you

   *  can  pass  in  constAll(c)

   */

#define  constAll(c)  c.constBegin(),  c.constEnd()

/**

   *  shorthand  to  writing  a  range  with  a  c.cBegin  and  c.cEnd.  Instead,  you

   *  can  pass  in  ccAll(c)

   */

#define  cAll(c)  c.cBegin(),  c.cEnd()

/**

   *  shorthand  to  writing  a  range  with  a  c.begin  and  c.end.  Instead,  you

   *  can  pass  in  all(c)

   */

#define  all(c)  c.begin(),  c.end()

Of course, if you use C++/11, you should probably be using begin(c) and end(c) instead of c.begin() and c.end()

You can then write:
std::sort(all(myList));

instead of:
std::sort(begin(myList), end(myList));
or
std::sort(myList.begin(), myList.end());

Of course, if you actually need to operate on part of a container, you can still use the iterators directly.

André




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150417/ca571090/attachment.html>


More information about the Interest mailing list