C++ Algorithm generate_n()30 Aug 2024 | 3 min read C++ Algorithm generate_n() function is used to assign the values which is generated by a function object to a specified number of elements in a range and returns to the one past the last assigned value position. The generator function is defined by the user and it is called successively for assigning the numbers. SyntaxParameterfirst: A forward iterator pointing the position of the first element in the range to which values are to be assigned. gen: A function object with no arguments that is used to generate the values to be assigned to each of the elements in the range. n: Number of elements to be assigned by generator function. It may be signed or unsigned integer type. Return valueNone ComplexityComplexity is linear in n. It calls gen and performs an assignment for each element. Data racesThe first n object in the range pointed by first are modified where each object is modified exactly once. Exception safetyThis function throws an exception if any of gen, 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 generate_n(): Output: 1,2,4,8,16,32,64,128,256,512, Example 2Let's see another simple example: Output: Vector v1 is ( 1804289383 846930886 1681692777 0 0 ). Deque deq1 is ( 1714636915 1957747793 424238335 719885386 0 ). Example 3Let's see another simple example: Output: 1 2 3 4 5 6 7 8 9 10 Example 4Let's see another simple example: Output: myarray contains: 1 2 3 4 5 6 7 8 9 Next TopicC++ Algorithm |
algorithm search() function
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
algorithm remove() function
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
algorithm replace_if() function
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
algorithm partition() function
C++ Algorithm partition() C++ Algorithm partition() function is used to make partition the elements on the basis of given predicate (condition) mentioned in its arguments. If the container is partitioned then this function returns true, else returns false. Syntax template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate...
5 min read
algorithm stable_sort() function
C++ Algorithm stable_sort() C++ Algorithm stable_sort() function is used to sort the elements in the range [first, last) into ascending order like sort but keeps the order of equivalent elements. The elements are compared using operator < for the first version, and comp for the second version. Syntax template <class...
6 min read
algorithm random_shuffle() function
C++ Algorithm random_shuffle() C++ Algorithm random_shuffle() reorders the elements of a range by putting them at random places. The first version uses an internal random number generator and the second version uses a random number generator which is a special kind of function object that is explicitly...
4 min read
algorithm unique_copy() function
C++ Algorithm unique_copy() C++ Algorithm unique_copy() function is used to copy a sequence such as each duplicate consecutive element becomes a unique element. It will not alter the original range and copy the result into another container. The first version uses operator== to compare the elements and the second...
6 min read
algorithm copy_n() function
C++ Algorithm Functions copy_n() C++ Algorithm copy_n() function specifies the number of elements to be copied into the new container. The function is used to copy n elements of the container [first,last) into a different container starting from result. Syntax template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n(InputIterator first, Size...
1 min read
algorithm remove_copy() function
C++ Algorithm remove_copy() C++ Algorithm remove_copy() function is used to copy all elements which are not equal to val from the range [first, last) to provide result without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator...
4 min read
algorithm copy() function
C++ Algorithm Function copy() C++ Algorithm copy() function is used to copy all the elements of the container [first,last] into a different container starting from result. Syntax template<class InputIterator, class OutputIterator>OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is an input iterator to the first element of the range,...
1 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