beautypg.com

Type definitions – HP Integrity NonStop H-Series User Manual

Page 68

background image

output operations performed by the copy operation into list insertions. (

See Chapter 2:

Insert Iterators

.)

The inserter requires two arguments; the list into which the value is to be inserted, and an iterator
indicating the location at which values will be placed. Insert iterators can also be used to copy
elements into an arbitrary location in an existing list.

list list_seven (aVector.begin(), aVector.end());

// the following is equivalent to the above
list list_eight;
copy (aVector.begin(), aVector.end(),
inserter(list_eight, list_eight.begin()));

The insert() operation, to be described in

Placing Elements into a List

, can also be used to place

values denoted by an iterator into a list. Insert iterators can be used to initialize a list with a sequence
of values produced by a generator (see

Chapter 13:

Initialize a Sequence with Generated Values

). This is

illustrated by the following:

list list_nine;
// initialize list 1 2 3 ... 7
generate_n (inserter(list_nine, list_nine.begin()),
7, iotaGen(1));

A copy constructor can be used to initialize a list with values drawn from another list. The
assignment operator performs the same actions. In both cases the assignment operator for the element
type is used to copy each new value.

list list_ten (list_nine); // copy constructor
list list_eleven;
list_eleven = list_six; // values copied by assignment

The assign() member function is similar to the assignment operator, 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. If a destructor is provided for the
container element type, it will be invoked for the elements being removed. 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 the number of elements specified by the count, which will be equal to either the
default value for the container type or the initial value specified.

list_six.assign(list_ten.begin(), list_ten.end());
list_four.assign(3, 7); // three copies of value seven
list_five.assign(12); // twelve copies of value zero

Finally, two lists can exchange their entire contents by means of the operation swap(). 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.

list_ten.swap(list_nine); // exchange lists nine and ten

This manual is related to the following products: