array
sAn array
is a consecutive group of memory locations that share the same type.
Each array
knows its own size, which can be determined by calling its size
member function (p. 285).
To refer to a particular location or element in an array
, we specify the name of the array
(p. 285) and the position number of the particular element in the array
.
A program refers to any one of an array
’s elements by giving the name of the array
followed by the index (p. 284) of the particular element in square brackets ([]
).
The first element in every array
has index zero (p. 285) and is sometimes called the zeroth element.
An index must be an integer or integer expression (using any integral type).
The brackets used to enclose the index are an operator with the same precedence as parentheses.
array
sarray
s occupy space in memory. You specify the type of each element and the number of elements required by an array
as follows:
array< type, arraySize> arrayName;
and the compiler reserves the appropriate amount of memory.
array
s can be declared to contain almost any data type. For example, an array
of type char
can be used to store a character string.
array
sThe elements of an array
can be initialized in the array
declaration by following the array
name with an initializer list (p. 288)—a comma-separated list (enclosed in braces) of initializers (p. 288).
When initializing an array
with an initializer list, if there are fewer initializers than elements in the array
, the remaining elements are initialized to zero. The number of initializers must be less than or equal to the array
size.
A constant variable that’s used to specify an array
’s size must be initialized when it’s declared and cannot be modified thereafter.
C++ has no array
bounds checking (p. 295) by default. You should ensure that all array
references remain within the bounds of the array
.
A static
local variable in a function definition exists for the duration of the program but is visible only in the function body.
A program initializes static
local array
s when their declarations are first encountered. If a static
array
is not initialized explicitly by you, each element of that array
is initialized to zero by the compiler when the array
is created.
for
StatementThe new C++11 range-based for
statement (p. 298) allows to manipulate all the elements of an array
without using a counter, thus avoiding the possibility of “stepping outside” the array
and eliminating the need for you to implement your own bounds checking.
A range-based for
statement’s header contains a range variable declaration to the left of the colon and the name of a container to the right, as in
for (rangeVariableDeclaration : container)
The range variable represents successive elements on successive iterations of the loop.
You can use the range-based for
statement with most of the C++ Standard Library’s prebuilt data structures (commonly called containers).
You can use a range-based for
statement to modify each element by making the range variable a reference.
The range-based for
statement can be used in place of the counter-controlled for
statement whenever code looping through an array
does not require access to the element’s subscript.
GradeBook
Using an array
to Store GradesClass variables (static
data members; p. 304) are shared by all objects of the class in which the variables are declared.
A static
data member can be accessed within the class definition and the member-function definitions like any other data member.
A public
static
data member can also be accessed outside of the class, even when no objects of the class exist, using the class name followed by the scope resolution operator (::
) and the name of the data member.
array
sSorting data—placing it into ascending or descending order—is one of the most important computing applications.
The process of finding a particular element of an array
is called searching.
C++ Standard Library function sort
sorts an array
’s elements into ascending order. The function’s arguments specify the range of elements that should be sorted. You’ll see that function sort
can be used on other types of containers too.
C++ Standard Library function binary_search
determines whether a value is in an array
. The sequence of values must be sorted in ascending order first. The function’s first two arguments represent the range of elements to search and the third is the search key—the value to locate. The function returns a bool
indicating whether the value was found.
array
sMultidimensional array
s (p. 308) with two dimensions are often used to represent tables of values (p. 307) consisting of information arranged in rows and columns.
array
s that require two subscripts to identify a particular element are called two-dimensional array
s (p. 308). An array
with m rows and n columns is called an m-by-n array
(p. 308).
GradeBook
Using a Two-Dimensional array
In a variable declaration, the keyword auto
(p. 309) can be used in place of a type name to infer the variable’s type based on the variable’s initializer value.
vector
C++ Standard Library class template vector
(p. 317) is similar to array
but also supports resizing.
By default, all the elements of an integer vector
object are set to 0.
A vector
can be defined to store any data type using a declaration of the form
vector<type> name(size);
Member function size
(p. 320) of class template vector
returns the number of elements in the vector
on which it’s invoked.
The value of an element of a vector
can be accessed or modified using square brackets ([]
).
Objects of standard class template vector
can be compared directly with the equality (==
) and inequality (!=
) operators. The assignment (=
) operator can also be used with vector
objects.
A nonmodifiable lvalue (a const
reference) is an expression that identifies an object in memory (such as an element in a vector), but cannot be used to modify that object. A modifiable lvalue (a non-const
reference) also identifies an object in memory, but can be used to modify the object.
An exception (p. 321) indicates a problem that occurs while a program executes. The name “exception” suggests that the problem occurs infrequently—if the “rule” is that a statement normally executes correctly, then the problem represents the “exception to the rule.”
Exception handling (p. 321) enables you to create fault-tolerant programs (p. 321) that can resolve exceptions.
To handle an exception, place the code that might throw an exception (p. 322) in a try
statement.
The try
block (p. 322) contains the code that might throw an exception, and the catch
block (p. 322) contains the code that handles the exception if one occurs.
When a try
block terminates, any variables declared in the try
block go out of scope.
A catch
block declares a type and an exception parameter. Inside the catch
block, you can use the parameter’s identifier to interact with a caught exception object.
An exception object’s what
member function (p. 322) returns the exception’s error message.