beautypg.com

Initialization algorithms, Fill a sequence with an initial value, Chapter 13 – HP Integrity NonStop H-Series User Manual

Page 144

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Initialization Algorithms

Obtaining the source

The first set of algorithms we will cover are those that are chiefly, although not exclusively, used to
initialize a newly created sequence with certain values. The standard library provides several
initialization algorithms. In our discussion we'll provide examples of how to apply these algorithms,
and suggest how to choose one algorithm over another.

Fill a Sequence with An Initial Value

The fill() and fill_n() algorithms are used to initialize or reinitialize a sequence with a fixed value.
Their declarations are as follows:

void fill (ForwardIterator first, ForwardIterator last, const T&);
void fill_n (OutputIterator, Size, const T&);

Different Initialization Algorithms

The example program illustrates several uses of the algorithm:

void fill_example ()
// illustrate the use of the fill algorithm
{
// example 1, fill an array with initial values
char buffer[100], * bufferp = buffer;
fill (bufferp, bufferp + 100, '\0');
fill_n (bufferp, 10, 'x');

// example 2, use fill to initialize a list
list aList(5, "nothing");
fill_n (inserter(aList, aList.begin()), 10, "empty");

// example 3, use fill to overwrite values in list
fill (aList.begin(), aList.end(), "full");

// example 4, fill in a portion of a collection
vector iVec(10);
generate (iVec.begin(), iVec.end(), iotaGen(1));

This manual is related to the following products: