C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
Algorithm transform - Generates a sequence of elements by applying a unary function
Algorithm transform - Generates a sequence of elements by applying a unary function transform Header <algorithm> template<class InputIterator, class OutputIterator, class UnaryOperator> OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperator op) Generates a sequence of elements by applying a unary function op to each element of the range [first, last). It copies the resulting sequence in the range starting result. template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperator> OutputIterator transform(InputIterator first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperator op) Generates a sequence of elements by applying binary function op to corresponding elements in the range [first1, last1) and elements in the range starting at first2 upto N elements. N = first1 - last1. It copies the resulting sequence in the range starting result. Sample #pragma warning(disable : 4786) #include <algorithm> #include <iostream> #include <functional> int main() { int array1[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9} ; int array2[9] , array3[9] ; std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ; std::cout << "\n\narray1 = " ; std::copy(array1, array1+9, intOstreamIt) ; std::cout << std::endl ; //transform //initialize array2 with negatives of elements in array1 std::transform(array1, array1+9, array2, std::negate<int>()) ; std::cout << "\n\narray2, after transform = " ; std::copy(array2, array2+9, intOstreamIt) ; std::cout << std::endl ; //initialize array3 with product of elements in array1 and array2 std::transform(array1, array1+9, array2, array3, std::multiplies<int>()) ; std::cout << "\n\narray3, after transform = " ; std::copy(array3, array3+9, intOstreamIt) ; std::cout << std::endl ; return 0 ; } Program Output array1 = 1, 2, 3, 4, 5, 6, 7, 8, 9, array2, after transform = -1, -2, -3, -4, -5, -6, -7, -8, -9, array3, after transform = -1, -4, -9, -16, -25, -36, -49, -64, -81,
Privacy Policy
|
Link to Us
|
Links