Chapter 4: Lists

Whenever possible, you should use lists instead of arrays. This chapter introduces containers and discusses various ways of grouping things in lists and how to iterate through them.

4.1 Introduction to Containers

There are many occasions when it is necessary to deal with collections of things. The classic approach in languages like C is to use arrays (Section 21.4) to store such collections. In C++, arrays are regarded as evil.

Following are a few good reasons to avoid using arrays in C++:

• Neither the compiler nor the runtime system checks array subscripts to make sure that they are within the correct range.

• A programmer using an array has the responsibility to write extra code to do the range checking.

• Arrays are either ...

Get Introduction to Design Patterns in C++ with Qt, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.