diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-10-10 08:51:04 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2019-05-07 14:15:37 +0000 |
commit | d510e1e7f919e01a3b875ff5a575b818e5ee03e8 (patch) | |
tree | 7ce9828d3e7f2867477736f2609762fb2977a56c /src | |
parent | 587bdd144b7e574e57f17167808774fa9783ee78 (diff) |
Add swapItemsAt() to QVector
This closes one compatibility gap with QList, to make
it easier to replace QList with QVector in Qt6.
Change-Id: I5655bc4cd2150a6f09a1ed68c0742f3b42ca47e4
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qvector.h | 7 | ||||
-rw-r--r-- | src/corelib/tools/qvector.qdoc | 10 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 4e148c9c55..b763d6e7e2 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -251,6 +251,13 @@ public: T value(int i) const; T value(int i, const T &defaultValue) const; + void swapItemsAt(int i, int j) { + Q_ASSERT_X(i >= 0 && i < size() && j >= 0 && j < size(), + "QVector<T>::swap", "index out of range"); + detach(); + qSwap(d->begin()[i], d->begin()[j]); + } + // STL compatibility typedef T value_type; typedef value_type* pointer; diff --git a/src/corelib/tools/qvector.qdoc b/src/corelib/tools/qvector.qdoc index cb47d36356..c1b5054f93 100644 --- a/src/corelib/tools/qvector.qdoc +++ b/src/corelib/tools/qvector.qdoc @@ -288,6 +288,16 @@ never fails. */ +/*! \fn template <typename T> void QVector<T>::swapItemsAt(int i, int j) + \since 5.14 + + Exchange the item at index position \a i with the item at index + position \a j. This function assumes that both \a i and \a j are + at least 0 but less than size(). To avoid failure, test that both + \a i and \a j are at least 0 and less than size(). +*/ + + /*! \fn template <typename T> bool QVector<T>::operator==(const QVector<T> &other) const Returns \c true if \a other is equal to this vector; otherwise |