This chapter began our introduction to data structures, exploring the use of C++ Standard Library class templates array
and vector
to store data in and retrieve data from lists and tables of values. The chapter examples demonstrated how to declare an array
, initialize an array
and refer to individual elements of an array
. We passed array
s to functions by reference and used the const
qualifier to prevent the called function from modifying the array
’s elements, thus enforcing the principle of least privilege. You learned how to use C++11’s range-based for
statement to manipulate all the elements of an array
. We also showed how to use C++ Standard Library functions sort
and binary_search
to sort and search an array
, respectively. You learned how to declare and manipulate multidimensional array
s of array
s. We used nested counter-controlled and nested range-based for
statements to iterate through all the rows and columns of a two-dimensional array
. We also showed how to use auto
to infer a variable’s type based on its initializer value. Finally, we demonstrated the capabilities of C++ Standard Library class template vector
. In that example, we discussed how to access array
and vector
elements with bounds checking and demonstrated basic exception-handling concepts. In later chapters, we’ll continue our coverage of data structures.
We’ve now introduced the basic concepts of classes, objects, control statements, functions, array
objects and vector
objects. In Chapter 8, we present one of C++’s most powerful features—the pointer. Pointers keep track of where data and functions are stored in memory, which allows us to manipulate those items in interesting ways. As you’ll see, C++ also provides a language element called an array (different from the class template array
) that’s closely related to pointers. In contemporary C++ code, its considered better practice to use C++11’s array
class template rather than traditional arrays.