transform() in C++ STL Last Updated : 11 Mar, 2025 Comments Improve Suggest changes Like Article Like Report In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Let’s take a look at a simple example that shows the how to use this function: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 5, 6, 8}; vector<int> v2(v1.size()); // Incrementing all elements of vector by 1 transform(v1.begin(), v1.end(), v2.begin(), [](int a) { return a + 1; }); for (auto i : v2) cout << i << " "; return 0; } Output2 6 7 9 Explanation: The function adds 1 to all elements of vector v1 and store the result in another vector v2.This article covers the syntax, usage, and common examples of transform() method in C++:Table of ContentSyntax of transform()Examples of transform()Decrementing the Every Value of Vector by 5 (Unary Operation)Convert all Characters of String to Lowercase (Unary Operation)Adding all Elements of Vector with One Another (Binary Operation)transform() in C++ - FAQsSyntax of transform()The transform() function is defined inside <algorithm> header file has 2 implementations:transform(first, last, res, op1); // For Unary Operationtransform(first, last, first1, res, op2); // For Binary OperationParametersfirst: Iterator to the first element of the first input range.last: Iterator to the element just after the last element of the first input range.first1: Iterator to the first element of second input range.res: Iterator to the beginning of the output range.op1: A unary operation to be performed.op2: A binary operation to be performed.Return ValueReturns an iterator to element just after the last element of output range.Examples of transform()The transform() function can be used to perform a wide variety of operations on the given range of operations. The following examples demonstrates some of the common examples that illustrates the use of this function.Decrementing Every Value of Array by 5 (Unary Operation) C++ #include <bits/stdc++.h> using namespace std; int main() { int arr1[5] = {6, 7, 8, 9, 10}; int n = sizeof(arr1)/sizeof(arr1[0]); int arr2[n]; // Decrementing every element of array by 5 transform(arr1, arr1 + n, arr2, [](int a) { return a - 5; }); for (auto i : arr2) cout << i << " "; return 0; } Output1 2 3 4 5 Convert String to Lowercase (Unary Operation) C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "GEEKSFORGEEKS"; // Convert all characters to lower case transform(s.begin(), s.end(), s.begin(), [](char c) { return tolower(c); }); cout << s; return 0; } OutputgeeksforgeeksExplanation: The transform() function converts all character of string s to lowercase and store the resultant string back to string s.Adding Each Element of Vector with Another Vector Elements(Binary Operation) C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {6, 7, 8, 9, 10}; vector<int> v2 = {1, 4, 8, 9, 43}; vector<int> v(v1.size()); // Adding all elements of vector v1 and v2 transform(v1.begin(), v1.end(), v2.begin(), v.begin(), [](int a, int b) { return a + b; }); for (auto i : v) cout << i << " "; return 0; } Output7 11 16 18 53 Explanation: The transform() function adds all elements of vector v1 and v2 with one another and store the resultant vector in v. Comment More infoAdvertise with us Next Article Variadic function templates in C++ K kartik Follow Improve Article Tags : C Language C++ STL CPP-Library cpp-algorithm-library +1 More Practice Tags : CPPSTL Similar Reads C++ STL Algorithm Library Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste 3 min read sort() in C++ STL In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include 4 min read Type Inference in C++ (auto and decltype) Type Inference is a feature in C++, using which the compiler automatically deduces the data type of an expression, function, or variable. Type inference was introduced with C++11 through the use of the auto and decltype.Before C++ 11, each data type had to be explicitly declared, which limited the v 5 min read transform() in C++ STL In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i 4 min read Variadic function templates in C++ Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor 3 min read Template Specialization in C++ Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p 5 min read Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge, Some of the merge operation classes are provided in C++ STL under the header file "algorithm", which facilitates several merge operations in a easy manner. Some of them are mentioned below. merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container 7 min read std::partition in C++ STL C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa 5 min read accumulate() and partial_sum() in C++ STL accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> hea 4 min read numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota()) The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc. 4 min read Like