Skip to content

Commit

Permalink
Add sections about std containers and algorithms
Browse files Browse the repository at this point in the history
Add section comparing Qt containers and std containers.

Add snippets showing use of std algorithms with Qt containers.

Task-number: QTBUG-86584
Pick-to: 6.0
Change-Id: I1133a5214a5acd086c37658ca11ab205a19a489b
Reviewed-by: Paul Wicking <[email protected]>
  • Loading branch information
olemd committed Dec 17, 2020
1 parent 970e54c commit 8183086
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/corelib/doc/snippets/code/doc_src_containers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,24 @@ QSet<int> set(list.begin(), list.end());
Will generate a QSet containing 1, 2, 3, 4, 5.
*/
//! [25]

//! [26]
QList<int> list { 2, 3, 1 };

std::sort(list.begin(), list.end());
/*
Sort the list, now contains { 1, 2, 3 }
*/

std::reverse(list.begin(), list.end());
/*
Reverse the list, now contains { 3, 2, 1 }
*/

int even_elements =
std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); });
/*
Count how many elements that are even numbers, 1
*/

//! [26]
51 changes: 51 additions & 0 deletions src/corelib/doc/src/containers.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,57 @@
\note The alternative macros Q_FOREACH and Q_FOREVER remain defined
regardless.

\section1 Qt containers compared with std containers

\table
\header \li Qt container \li Closest std container

\row \li \l{QList}<T>
\li Similar to std::vector<T>

\l{QList} and \l{QVector} were unified in Qt 6. Both
use the datamodel from QVector. QVector is now an alias to QList.

This means that QList is not implemented as a linked list, so if
you need constant time insert, delete, append or prepend,
consider \c std::list<T>. See \l{QList} for details.

\row \li \l{QVarLengthArray}<T, Prealloc>
\li Resembles a mix of std::array<T> and std::vector<T>.

For performance reasons, QVarLengthArray lives on the stack unless
resized. Resizing it automatically causes it to use the heap instead.

\row \li \l{QStack}<T>
\li Similar to std::stack<T>, inherits from \l{QList}.

\row \li \l{QQueue}<T>
\li Similar to std::queue<T>, inherits from \l{QList}.

\row \li \l{QSet}<T>
\li Similar to std::set<T>. Internally, \l{QSet} is implemented with a
\l{QHash}.

\row \li \l{QMap}<Key, T>
\li Similar to std::map<T>.

\row \li \l{QMultiMap}<Key, T>
\li Similar to std::multimap<T>.

\row \li \l{QHash}<Key, T>
\li Most similar to std::map<T>.

\row \li \l{QMultiHash}<Key, T>
\li Most similar to std::multimap<T>.

\endtable

\section1 Qt containers and std algorithms

You can used Qt containers with functions from \c{#include <algorithm>}.

\snippet code/doc_src_containers.cpp 26

\section1 Other Container-Like Classes

Qt includes other template classes that resemble containers in
Expand Down

0 comments on commit 8183086

Please sign in to comment.