C++ Algorithm fill()30 Aug 2024 | 3 min read 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 |
C++ Algorithm Function search() C++ Algorithm search() function searches the range [first1, last1) for the occurrence of a subsequence defined by the range [first2, last2), and an iterator to the first element is returned. If the subsequence does not exist then an iterator to the last1 is...
2 min read
C++ Algorithm Function count() C++ Algorithm count()function accepts 'val' as argument and compares for the occurrence of element 'val' in the range. The number of occurrence of that element is returned. Syntax template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val); Parameter first: It is...
1 min read
C++ Algorithm partition_copy() C++ Algorithm partition_copy() function is used to copy the elements for which a condition is true to one destination, and false to another. The elements must belong to a specified range. Syntax template <class InputIterator, class OutputIterator1, ...
4 min read
C++ Algorithm replace_if() C++ Algorithm replace_if() function is used to assign new_value to all the elements in the range [first, last) for which pred predicate returns true. This function examines each element in a range and replaces it if it satisfies a specified predicate. Syntax template <class ForwardIterator,...
4 min read
C++ Algorithm rotate_copy() C++ Algorithm rotate_copy() function is used to make a rotated copy of the elements in the range [first, last). The sequence will start at the element in the middle of the source sequence and the last element will be followed by first. It appends the...
7 min read
C++ Algorithm set_union() C++ Algorithm set_union() function is used to find the union of two sorted ranges [first1, last1) and [first2, last2), which is formed by the elements that are present in either one of the sets or in both. Elements are compared using operator < for the...
6 min read
C++ Algorithm remove() C++ Algorithm remove() function is used to eliminate all the elements that are equal to val from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the...
5 min read
Introduction: In the dynamic realm of programming, the ability to harness the full potential of hardware resources is a key aspect. Multithreading, the simultaneous execution of multiple threads, plays a crucial role in achieving parallelism and enhancing performance. C++, being a versatile programming language, offers the...
7 min read
C++ Algorithm Function find() C++ Algorithm find() function specifies a value in the argument list, a search for that value is made in the range, the iterator starts the search from the first element and goes on to the last element, if the element is found in...
2 min read
C++ Algorithm Functions mismatch() C++ Algorithm mismatch() function compares both the containers to spot for any mismatch of values. The function returns the first element of both the containers that does not match. Syntax template<class InputIterator1, classInputIterator2> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator2 first2> template<class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1,InputIterator2> mismatch(InputIterator1 first1,...
3 min read
We request you to subscribe our newsletter for upcoming updates.
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