Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different efficiency of iterator and const_iterator (STL)

Tags:

c++

iterator

stl

In Qt there are similar classes to list an map. These classes provide a begin_const() method that returns a const_iterator. The documentation says that these const_iterators should be used whenever possible since they are faster.

The STL only gives you a const_iterator if the instance itself is const. Only one begin() method is implemented (overloaded for const).

Is there any difference when read-accessing elements with iterator and const_iterator? (I dont'w know why there's a difference for them in Qt)

like image 749
HWende Avatar asked Apr 30 '12 10:04

HWende


People also ask

What is the difference between iterator and const_iterator?

There is no performance difference. A const_iterator is an iterator that points to const value (like a const T* pointer); dereferencing it returns a reference to a constant value ( const T& ) and prevents modification of the referenced value: it enforces const -correctness.

What is iterator in STL?

An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container.

What is the difference between Begin and Cbegin?

begin() returns an iterator to beginning while cbegin() returns a const_iterator to beginning. The basic difference between these two is iterator (i.e begin() ) lets you change the value of the object it is pointing to and const_iterator will not let you change the value of the object.

What is constant iterator?

Constant Iterators: A const iterator points to an element of constant type which means the element which is being pointed to by a const_iterator can't be modified. Though we can still update the iterator (i.e., the iterator can be incremented or decremented but the element it points to can not be changed).


1 Answers

The documentation says that these const_iterators should be used whenever possible since they are faster.

It sure does. From http://qt-project.org/doc/qt-4.8/containers.html#stl-style-iterators:

For each container class, there are two STL-style iterator types: one that provides read-only access and one that provides read-write access. Read-only iterators should be used wherever possible because they are faster than read-write iterators.

What a stupid thing to say.

Safer? Yes. Faster? Even if this were the case (it apparently isn't with gcc and clang), it is rarely a reason to prefer const iterators over non-const ones. This is premature optimization. The reason to prefer const iterators over non-const ones is safety. If you don't need the pointed-to contents to be modified, use a const iterator. Think of what some maintenance programmer will do to your code.

As far as begin versus cbegin is concerned, that's a C++11 addition. This allows the auto keyword to use a const iterator, even in a non-const setting.

like image 59
David Hammen Avatar answered Nov 03 '22 01:11

David Hammen