Home > Blogs > 30 C++ Tips in 30 Days: Tip # 23 iterators are so 1998

30 C++ Tips in 30 Days: Tip # 23 iterators are so 1998

By  Dec 12, 2007

Topics: Programming, C/C++

A spoiler warning: don't read this tip if you want to keep the topic of my next C++ newsletter as a surprise!

The C++03 standard defines a range as "a pair of iterators that designate the beginning and end of the computation". This seemingly trivial notion is going to change the way we use STL algorithm, containers and even built-in arrays, making our code simpler to read and write, safer and in some cases even more efficient.

The range library is already widely used among Boosters. A revised version thereof is now being reviewed before it gets integrated into the Working Paper of standard C++. Since the whole notion of ranges is so intuitive that I see no reason why it won't be incorporated into C++09.

Standard algorithms are specified in terms of iterators, which makes them somewhat clumsy to use. By combining two iterators into one object, C++ programmers and library designers can achieve great benefits:

  1. Reducing syntactic overhead
  2. Providing an infrastructure for range-based algorithms (as well as a new for-loop syntax),
  3. Safer use of built-in arrays
  4. Creating range-based algorithms that offer superior performance and flexibility compared to iterator-based algorithms

Find out how these benefits are accomplished in my next C++ newsletter.

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.