The documentation says that these constants should be used whenever possible because they are faster.
Of course. From http://qt-project.org/doc/qt-4.8/containers.html#stl-style-iterators:
For each container class, there are two types of STL type iterators: one that provides read-only access, and one that provides read and write access. Read-only iterators should be used wherever possible because they are faster than read and write iterators.
What a stupid thing to say.
Safer? Yes. Faster? Even if that were the case (this is apparently not with gcc and clang), it is rarely preferable to use constant iterators over non-constant ones. This is premature optimization. The reason for the preference of constant iterators over non-constant is security. If you donβt need editable content, use a constant iterator. Think about what some programmer will do with your code.
As for begin
compared to cbegin
, this is a C ++ 11 add-on. This allows the auto
keyword to use a constant iterator even in non-constant settings.
source share