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.SyntaxParameterfirst: 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 valueNone ComplexityComplexity is linear in the distance within first and last and assignment for each element. Data racesThe objects in the range [first1, last1) are modified where each object is accessed exactly once. Exception safetyThis 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 1Let's see the simple example to demonstrate the use of fill(): Output: 2,2,2,2,2, Example 2Let's see another simple example: Output: myvector contains: 5 5 5 8 8 8 0 0 Example 3Let'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 4Let'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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India