cbegin()/ cend()vs constBegin()/ constEnd()

Chr*_*eis 7 c++ qt iterator qt5

Qt 5.0引入了迭代器方法,cbegin()并引入了cend()不同的容器类,如QListQMap.

但是这些课程中也有方法constBegin()constEnd()方法.

所有这些方法都是constSTL风格的const_iterator.

  • 难道cbegin()/ cend()比相同的功能constBegin()/ constEnd()?对我来说似乎是这样,但在QList,QMap容器类的文档中没有说明任何内容.
  • 是否有人应该使用cbegin()/ cend()而不是constBegin()/ constEnd()反之亦然?

Hco*_*org 11

cbegin() and cend() where introduced for compatibility with Standard Library containers, which all contain such functions since C++11.
Qt just wants to keep it interface similar to the standard library. constBegin() etc. are just older versions (Qt added them before C++11 was released). There is no difference in using them.

我会使用constBegin(),constEnd()因为它们更明确和'Qt风格',但这只是我个人的偏好.cbegin()/ cend()可能被一些为标准容器实现的算法使用(因此它们存在于Qt中 - 它们有助于重用一些代码).如果您希望在某些时候想要在Qt之外重用代码,请使用它们.

  • 使用cbegin等。使将来更容易用标准容器类型替换Qt类型。在我看来,Qt已经改变了如何做事的主意。如今,他们曾经说过:只有Qt功能可用于所有用途的思维定式:在最好的地方使用标准功能,在最好的地方使用Qt功能。 (2认同)