C++ Algorithm transform()17 Mar 2025 | 4 min read C++ Algorithm transform() function is used in two different ways: 1.unary operation:- This method performs unary operation op on the elements in range [first1, last1] and stores the result in range starting from result. This transform() applies a function to each element of a range: ![]() 2.Binary operation:- This method performs binary operation binary_op on the elements in range [first1, last1] with the elements in the range starting with iterator first2 and stores the result in range starting from result. This transform() takes two 2 ranges and applies a function that takes 2 parameters, on each couple of elements from the input ranges: ![]() Syntaxunary operation(1) Binary operation(2) Parameterfirst1: An input iterator pointing the position of the first element of the first range to be operated on. last1: An iterator pointing the position one past the final element of the first range to be operated on. first2: Input iterator pointing to the first element in the second range to be operated on. result: An output iterator to the initial position of the range where the operation results are stored. op: Unary function applied to each element of the range. binary_op: Binary function that two elements passed as its arguments. Return valuetransform() returns an iterator pointing to the end of the transformed range. ComplexityComplexity is linear in the distance between first1 and last1. Data racesThe objects in the range [first1, last1) are accessed where each object is accessed exactly once. The object in the range beginning at result is modified. Exception safetyThrows an exception if any of the function calls the assignments or the operations on iterators throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of transform(): Output: 6 2 8 Example 2Let's see another simple example: Output: aaa b cccc Example 3Let's see another simple example: Output: Transform operation b[i] a[i] c[i] 1 ^ 1 = 1 2 ^ 2 = 4 3 ^ 1 = 3 1 ^ 2 = 1 2 ^ 1 = 2 3 ^ 2 = 9 1 ^ 1 = 1 2 ^ 2 = 4 3 ^ 1 = 3 1 ^ 2 = 1 The above example illustrates the transform() algorithm. The program creates two vectors and transforms the third vector by inserting a value equal to an element from first vector raise to the power of element in second vector. The function power is passed as a predicate to the function transform(). Example 4Let's see another simple example: Output: foo contains: 21 41 61 81 101 Next TopicC++ Algorithm |
C++ Algorithm is_sorted_until() C++ Algorithm is_sorted_until() function is used to find first unsorted element in the range. It means it returns an iterator to the first element in the range [first, last) which does not follow an ascending order. The elements are compared using operator < for...
4 min read
C++ Algorithm is_sorted() C++ Algorithm is_sorted() function returns true if the elements in the range [first, last) are sorted into ascending order. The elements are compared using operator < for the first version, and comp for the second version. Syntax default (1) template <class ForwardIterator> ...
4 min read
C++ Algorithm Function copy_if() C++ Algorithm copy_if() function is used to copy the elements of the container [first,last] into a different container starting from result for which the value of pred is true. Syntax template<class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result,UnaryPredicate pred); Parameter first: It is...
1 min read
C++ Algorithm upper_bound() C++ Algorithm upper_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in the range [first, last) that is greater than to the specified value val. The first version uses operator < to compare...
5 min read
C++ Algorithm is_partitioned() C++ Algorithm is_partitioned() is used to test to see if a range [first, last) is partitioned according to a predicate. In other words, all the elements in the range that satisfies the predicate are at the beginning of the sequence. If the range is empty...
4 min read
C++ Algorithm lower_bound() C++ Algorithm lower_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in an ordered range [first, last) that is not less than (i.e. greater than or equal to) to the specified value...
4 min read
C++ Algorithm Functions none_of() C++ Algorithm none_of() function returns a true value if the value of 'pred' argument is false. The value should be false for all elements in the range [first, last). Syntax template <class InputIterator, class UnaryPredicate> bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first : It specifies...
2 min read
C++ Algorithm stable_partition() C++ Algorithm stable_partition() function is used to classify the elements in the range [first, last), in such a way that all the elements for which pred returns true precede all those for which it returns false, where the relative order of elements is preserved. Note:...
4 min read
C++ Algorithm generate () C++ Algorithm generate() function is used to assign the value generated by a function object to each element in a range. The generator function is defined by the user and it is called successively for assigning the numbers. Syntax template <class ForwardIterator, class Generator> void...
4 min read
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
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