beautypg.com

Type definitions – HP Integrity NonStop H-Series User Manual

Page 57

background image

Constructors and Iterators

A vector can be assigned the values of another vector, in which case the target receives a copy of
the argument vector.

vec_three = vec_five;

The assign() member function is similar to an assignment, but is more versatile and, in some cases,
requires more arguments. Like an assignment, the existing values in the container are deleted, and
replaced with the values specified by the arguments. There are two forms of assign(). The first takes
two iterator arguments that specify a subsequence of an existing container. The values from this
subsequence then become the new elements in the receiver. The second version of assign() takes a
count and an optional value of the container element type. After the call the container will hold only
the number of elements specified by the count, which are equal to either the default value for the
container type or the initial value specified.

vec_six.assign(list_ten.begin(), list_ten.end());
vec_four.assign(3, 7); // three copies of the value 7
vec_five.assign(12); // twelve copies of value zero

If a destructor is defined for the container element type, the destructor will be called for each value
removed from the collection.

Finally, two vectors can exchange their entire contents by means of the swap() operation. The
argument container will take on the values of the receiver, while the receiver will assume those of
the argument. A swap is very efficient, and should be used, where appropriate, in preference to an
explicit element-by-element transfer.

vec_three.swap(vec_four);

Type Definitions

The class

vector

includes a number of type definitions. These are most commonly used in

declaration statements. For example, an iterator for a vector of integers can be declared in the
following fashion:

vector::iterator location;

In addition to iterator, the following types are defined:

value_type

The type associated with the elements the vector maintains.

const_iterator

An iterator that does not allow modification of the underlying sequence.

reverse_iterator

An iterator that moves in a backward direction.

const_reverse_iterator A combination constant and reverse iterator.

reference

A reference to an underlying element.

const_reference

A reference to an underlying element that will not permit the element to
be modified.

size_type

An unsigned integer type, used to refer to the size of containers.

This manual is related to the following products: