beautypg.com

Subscripting a vector, Extent and size-changing operations – HP Integrity NonStop H-Series User Manual

Page 58

background image

difference_type

A signed integer type, used to describe distances between iterators.

Subscripting a Vector

The value being maintained by a vector at a specific index can be accessed or modified using the
subscript operator, just like an ordinary array. And, like arrays, there currently are no attempts to
verify the validity of the index values (although this may change in future releases). Indexing a
constant vector yields a constant reference. Attempts to index a vector outside the range of legal
values will generate unpredictable and spurious results:

cout << vec_five[1] << endl;
vec_five[1] = 17;

The member function at() can be used in place of the subscript operator. It takes exactly the same
arguments as the subscript operator, and returns exactly the same values.

The member function front() returns the first element in the vector, while the member function
back() yields the last. Both also return constant references when applied to constant vectors.

cout << vec_five.front() << " ... " << vec_five.back() << endl;

Extent and Size-Changing Operations

There are, in general, three different "sizes" associated with any vector. The first is the number of
elements currently being held by the vector. The second is the maximum size to which the vector
can be expanded without requiring that new storage be allocated. The third is the upper limit on the
size of any vector. These three values are yielded by the member functions size(), capacity(), and
max_size(), respectively.

cout << "size: " << vec_five.size() << endl;
cout << "capacity: " << vec_five.capacity() << endl;
cout << "max_size: " << vec_five.max_size() << endl;

The maximum size is usually limited only by the amount of available memory, or the largest value
that can be described by the data type size_type. The current size and capacity are more difficult to
characterize. As we will note in the next section, elements can be added to or removed from a
vector in a variety of ways. When elements are removed from a vector, the memory for the vector is
generally not reallocated, and thus the size is decreased but the capacity remains the same. A
subsequent insertion does not force a reallocation of new memory if the original capacity is not
exceeded.

Memory Management

An insertion that causes the size to exceed the capacity generally results in a new block of memory
being allocated to hold the vector elements. Values are then copied into this new memory using the
assignment operator appropriate to the element type, and the old memory is deleted. Because this
can be a potentially costly operation, the

vector

data type provides a means for the programmer to

specify a value for the capacity of a vector. The member function reserve() is a directive to the
vector, indicating that the vector is expected to grow to at least the given size. If the argument used

This manual is related to the following products: