Javatpoint Logo
Javatpoint Logo

C++ Algorithm fill()

C++ Algorithm fill() function is used to assign the same new value to every element in a specified range[first, end) by using operator=.

Note: Range [first, last) means first is included in the range but last is not included.

Syntax

Parameter

first: A forward iterator pointing the position of the first element in a specified range.

last: A forward iterator pointing the position one past the final element in the range to be traversed.

val: Value to be assigned to elements in the range [first, last).

Return value

None

Complexity

Complexity is linear in the distance within first and last and assignment for each element.

Data races

The objects in the range [first1, last1) are modified where each object is accessed exactly once.

Exception safety

This function throws an exception the element assignments or the operation on an iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of fill():

Output:

2,2,2,2,2,

Example 2

Let's see another simple example:

Output:

myvector contains: 5 5 5 8 8 8 0 0

Example 3

Let's see another simple example:

Output:

Vector v1 = ( 0 5 10 15 20 25 30 35 40 45 )
Modified v1 = ( 0 5 10 15 20 2 2 2 2 2 )

Example 4

Let's see another simple example:

Output:

Vector before fill
 0  1  2  3  4  5  6  7  8  9 
Vector after fill
 0  1  2  3 -1 -1 -1  7  8  9

Next TopicC++ Algorithm




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA