C++ Tutorial/STL Algorithms Modifying sequence operations/transform — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:31, 25 мая 2010
Содержание
- 1 multiplying the elements in v with those in v2
- 2 std::transform with predicate
- 3 Transform algorithm and list of integers
- 4 Transform all elements into deque from set by multiplying 10
- 5 Transform array to pair
- 6 Transform v, putting the result into a new sequence
- 7 Use generic template function with transform
- 8 Use transform function to square all elements in an array
- 9 Use transform to add each element traversed forward with each element traversed backward
- 10 Use transform to negate all elements in a container
- 11 Use transform to print differences of two corresponding elements
- 12 Use transform to print elements negatively and in reverse order
- 13 Use transform to square each element
- 14 Use transform to transform elements in one container into another container with ten times their value
multiplying the elements in v with those in v2
<source lang="cpp">#include <iostream>
- include <vector>
- include <algorithm>
- include <functional>
using namespace std; int main() {
vector<int> v, v2, result(10); for(unsigned i=0; i < 10; ++i) v.push_back(i); for(unsigned i=0; i < 10; ++i) v2.push_back(i); transform(v.begin(), v.end(), v2.begin(), result.begin(), multiplies<int>()); return 0;
}</source>
std::transform with predicate
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <algorithm>
- include <numeric>
- include <vector>
- include <iterator>
int calculateCube( int ); int main() {
std::ostream_iterator< int > output( cout, " " ); int a2[ 10 ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 }; std::vector< int > v2( a2, a2 + 10 ); // copy of a2 cout << "Vector v2 contains: "; std::copy( v2.begin(), v2.end(), output ); std::vector< int > cubes( 10 ); // instantiate vector cubes // calculate cube of each element in v; place results in cubes std::transform( v2.begin(), v2.end(), cubes.begin(), calculateCube ); cout << "\n\nThe cube of every integer in Vector v is:\n"; std::copy( cubes.begin(), cubes.end(), output );
cout << endl; return 0;
} int calculateCube( int value ) {
return value * value * value;
}</source>
Vector v2 contains: 100 2 8 1 50 3 8 8 9 10 The cube of every integer in Vector v is: 1000000 8 512 1 125000 27 512 512 729 1000
Transform algorithm and list of integers
<source lang="cpp">#include <iostream>
- include <list>
- include <algorithm>
using namespace std;
double reciprocal(double i) {
return 1.0/i;
}
int main(){
list<double> l; int i; for(i=1; i<10; i++) l.push_back((double)i); list<double>::iterator p = l.begin(); while(p != l.end()) { cout << *p << endl; p++; } p = transform(l.begin(), l.end(),l.begin(), reciprocal); p = l.begin(); while(p != l.end()) { cout << *p << endl; p++; } return 0;
}</source>
Transform all elements into deque from set by multiplying 10
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <set>
- include <deque>
- include <algorithm>
using namespace std; /* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
}
int main() {
set<int,greater<int> > coll1; deque<int> coll2; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll1.insert(i); } PRINT_ELEMENTS(coll1,"initialized: "); // transform all elements into coll2 by multiplying 10 transform (coll1.begin(),coll1.end(), // source back_inserter(coll2), // destination bind2nd(multiplies<int>(),10)); // operation PRINT_ELEMENTS(coll2,"transformed: ");
}</source>
initialized: 9 8 7 6 5 4 3 2 1 transformed: 90 80 70 60 50 40 30 20 10
Transform array to pair
<source lang="cpp">#include <algorithm>
- include <iostream>
- include <iterator>
- include <string>
- include <vector>
- include <utility>
using namespace std; typedef pair<int,string> Pair; inline bool less_than_second( const Pair& b1, const Pair& b2 ){
return b1.second < b2.second;
} int main( ) {
const char* names[] = { "A","B", "C", "D","E" }; const int values[] = { 18, 20, 26, 30, 41 }; const int num_pairs = sizeof( names ) / sizeof( names[0] ); vector<Pair> pair( num_pairs ); transform( values, values+num_pairs, names,pair.begin(), make_pair<int,string> ); if( pair[1].first > pair[3].first ) cout << pair[1].second << " > " << pair[3].second << endl; else if( pair[1].first == pair[3].first ) cout << pair[1].second << " == " << pair[3].second << endl; else cout << pair[1].second << " < " << pair[3].second << endl;
}</source>
Transform v, putting the result into a new sequence
<source lang="cpp">#include <iostream>
- include <vector>
- include <algorithm>
- include <functional>
using namespace std; // A function object that computes a reciprocal. class reciprocal : unary_function<double, double> { public:
result_type sum;
result_type operator()(argument_type val) {
if(val == 0.0) return 0.0; return 1.0 / val; // return reciprocal }
}; int main(){
int i; vector<double> v; for(i=1; i < 10; ++i) v.push_back((double)i); vector<double> v2(10); transform(v.begin(), v.end(), v2.begin(), reciprocal()); return 0;
}</source>
Use generic template function with transform
<source lang="cpp">/* The following code example is taken from the book
* "C++ Templates - The Complete Guide" * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002 * * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
using namespace std; template <typename T, int VAL> T addValue (T const& x) {
return x + VAL;
}
void call_addValue() {
addValue<int,5>(42);
} template <typename IT, typename OP> void transform (IT beg, IT end, IT to, OP op) {
while (beg != end) { *to++ = op(*beg++); }
} int main() {
call_addValue(); int m[] = { 1, 2, 3, 4, 5, 6 }; transform (m, m+6, m, (int(*)(int const&)) // cast necessary addValue<int,5>); for(int i=0;i<6;i++){ cout << m[i] << " "; }
}</source>
6 7 8 9 10 11 "
Use transform function to square all elements in an array
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <algorithm>
- include <functional>
- include <iterator>
using namespace std; int main() {
int coll[] = { 5, 6, 2, 4, 1, 3 }; // square all elements transform (coll, coll+6, // first source coll, // second source coll, // destination multiplies<int>()); // operation // print all elements copy (coll, coll+6, ostream_iterator<int>(cout," ")); cout << endl;
}</source>
25 36 4 16 1 9
Use transform to add each element traversed forward with each element traversed backward
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
vector<int> coll1; list<int> coll2; INSERT_ELEMENTS(coll1,1,9); PRINT_ELEMENTS(coll1,"coll1: "); /* add each element traversed forward with each element traversed backward * and insert result into coll2 */ transform (coll1.begin(), coll1.end(), // first source range coll1.rbegin(), // second source range back_inserter(coll2), // destination range plus<int>()); // operation PRINT_ELEMENTS(coll2,"coll2: ");
}</source>
coll1: 1 2 3 4 5 6 7 8 9 coll2: 10 10 10 10 10 10 10 10 10
Use transform to negate all elements in a container
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
vector<int> coll1; list<int> coll2; INSERT_ELEMENTS(coll1,1,9); PRINT_ELEMENTS(coll1,"coll1: "); // negate all elements in coll1 transform (coll1.begin(), coll1.end(), // source range coll1.begin(), // destination range negate<int>()); // operation PRINT_ELEMENTS(coll1,"negated: ");
}</source>
coll1: 1 2 3 4 5 6 7 8 9 negated: -1 -2 -3 -4 -5 -6 -7 -8 -9
Use transform to print differences of two corresponding elements
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
vector<int> coll1; list<int> coll2; INSERT_ELEMENTS(coll1,1,9); PRINT_ELEMENTS(coll1,"coll1: "); // print differences of two corresponding elements cout << "diff: "; transform (coll1.begin(), coll1.end(), // first source range coll2.begin(), // second source range ostream_iterator<int>(cout, " "), // destination range minus<int>()); // operation cout << endl;
}</source>
coll1: 1 2 3 4 5 6 7 8 9 diff: 2 3 4 5 6 7 8 9 10
Use transform to print elements negatively and in reverse order
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
vector<int> coll1; list<int> coll2; INSERT_ELEMENTS(coll1,1,9); PRINT_ELEMENTS(coll1,"coll1: "); // print coll2 negatively and in reverse order transform (coll2.rbegin(), coll2.rend(), // source range ostream_iterator<int>(cout," "), // destination range negate<int>()); // operation cout << endl;
}</source>
coll1: 1 2 3 4 5 6 7 8 9
Use transform to square each element
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
vector<int> coll1; list<int> coll2; INSERT_ELEMENTS(coll1,1,9); PRINT_ELEMENTS(coll1,"coll1: "); // square each element transform (coll1.begin(), coll1.end(), // first source range coll1.begin(), // second source range coll1.begin(), // destination range multiplies<int>()); // operation PRINT_ELEMENTS(coll1,"squared: ");
}</source>
coll1: 1 2 3 4 5 6 7 8 9 squared: 1 4 9 16 25 36 49 64 81
Use transform to transform elements in one container into another container with ten times their value
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
vector<int> coll1; list<int> coll2; INSERT_ELEMENTS(coll1,1,9); PRINT_ELEMENTS(coll1,"coll1: "); // transform elements of coll1 into coll2 with ten times their value transform (coll1.begin(), coll1.end(), // source range back_inserter(coll2), // destination range bind2nd(multiplies<int>(),10)); // operation PRINT_ELEMENTS(coll2,"coll2: ");
}</source>
coll1: 1 2 3 4 5 6 7 8 9 coll2: 10 20 30 40 50 60 70 80 90