Cbegin () / cend () vs constBegin () / constEnd ()

Qt 5.0 introduced the iterator methods cbegin() and cend() in different container classes such as QList or QMap .

But in these classes there are also methods constBegin() and constEnd() .

All these methods are const and return the STL style const_iterator .

  • Do cbegin() / cend() have the same functionality than constBegin() / constEnd() ? This is true for me, but nothing is said in the QList , QMap or container classes docs.
  • Is there a scenario in which cbegin() / cend() should be used instead of constBegin() / constEnd() or vice versa?
+5
source share
2 answers

cbegin() and cend() , where they are introduced for compatibility with standard library containers, all of which contain such functions, since C ++ 11.
Qt just wants to keep its interface similar to the standard library. constBegin() etc. - these are only older versions (Qt added them before the release of C ++ 11). There is no difference in their use.

I would use constBegin() and constEnd() as they are more explicit and β€œQt style”, but these are just my personal preferences. cbegin() / cend() can be used by some algorithms implemented for standard containers (hence their existence in Qt - they help reuse some code). Use them if you expect that at some point you will want to reuse your code outside of Qt.

+9
source

Qt usually often provides different ways to use things, so programmers can use the style that they use to use.

Symmetric case with iterator type. You can use the standard style used in iterators of the standard library or Java. This is for the convenience of the user.

The reason for cbegin and constBegin is simillar. In addition, if the documentation does not indicate a difference, then there is no difference.

As you noticed. constBegin is pure QtStyle, and cbegin is STL style.

+1
source

All Articles