90 likes | 105 Views
Explore combining STL containers, iterators, algorithms, and functors in C++ for efficient code writing and program building. Examples demonstrate generic feature combinations, like creating custom functors. Learn to apply operations on data sets without storing results. Utilize back insert iterators to perform transformations and output directly.
E N D
Combining STL Features • STL has containers, iterators, algorithms, and functors • With several to many different varieties of each • Today we’ll look at some combinations of features • With each other and with other parts of C++ and its libraries • The goal here is to illustrate and give examples • There’s no way we’ll cover everything you could do • But hopefully we’ll show that much more can be done • Combining STL features generically can be efficient • Write less code (and more importantly debug less code) • Build more sophisticated programs more easily
#include <algorithm> #include <vector> #include <functional> #include <iterator> #include <iostream> #include <cmath> using namespace std; Include libraries you need, e.g., Algorithms Vector container Functors Iterators Input/output streams C math functions Don’t forget to open namespace Think about the Kinds of Abstractions You Want
template <typename T> struct square_functor : public unary_function<T, T> { T operator () (T t) { return t * t; } }; We could use C pow() function But that only works with doubles Also, pow is binary, and the function f(x) = x2 is unary Instead, we can make our own generic functor template Squares any number Works with the STL algorithms Uses by-value call and return, to prevent possible memory issues Let’s Say We Want to Square Some Numbers
int int_array [] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; vector<int> int_squares; transform (int_array, int_array + sizeof(int_array)/sizeof(int), back_inserter(int_squares), square_functor<int>()); copy (int_squares.begin(), int_squares.end(), ostream_iterator<int>(cout, " ")); /* output: 1 4 9 16 25 36 49 64 81 */ Notice use of back inserter and functor Reads each value from the input range Applies the functor to each input value Uses back insertion iterator to push result into vector Applying Functors is Easy with Transform
int int_array [] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; transform (int_array, int_array + sizeof(int_array)/sizeof(int), ostream_iterator<int>(cout, " "), square_functor<int>()); /* same output: 1 4 9 16 25 36 49 64 81 */ If we just want to print results, no need to store them Can even get rid of the vector Can get rid of the back insertion iterator as well Now, the ostream iterator provides the output range Applying Functors is Easy with Transform
vector<double> fractions; back_insert_iterator<vector<double> > iter(fractions); *iter++ = 1.0; *iter++ = 2.0; *iter++ = 3.0; *iter++ = 4.0; *iter++ = 5.0; *iter++ = 6.0; *iter++ = 7.0; *iter++ = 8.0; *iter++ = 9.0; cout.precision(6); copy (fractions.begin(), fractions.end(), ostream_iterator<double>(cout, " ")); /* output: 1 2 3 4 5 6 7 8 9 */ A few more ideas Can declare a back insert iterator as a variable Can dereference and assign using that iterator Note that space is created in the vector for the values Note also the use of the precision method on cout What If We Want to Work With Another Type?
vector<double> squared_fractions; transform (fractions.begin(), fractions.end(), back_inserter(squared_fractions), square_functor<double>()); copy (squared_fractions.begin(), squared_fractions.end(), ostream_iterator<double>(cout, " ")); /* output: 1 4 9 16 25 36 49 64 81 */ We can apply the functor equally well to doubles This time a vector gives our input range Functor template is instantiated to square doubles Back inserter and output iterator work for doubles as well The Combinations Are All Appropriately Generic
transform (squared_fractions.begin(), squared_fractions.end(), ostream_iterator<double>(cout, " "), sqrt); /* output: 1 2 3 4 5 6 7 8 9 */ transform (fractions.begin(), fractions.end(), ostream_iterator<double>(cout, " "), sqrt); /* output: 1 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 */ What if we want to apply the inverse function? C sqrt function is unary, and can be used for doubles Notice that the precision we set for cout still is observed What Else Can We Combine?
Summary • We’ve looked at combining STL features with each other and with other library and language features • Certain details matter and must be considered • E.g., output precision or a library function’s arity and types • E.g., needing an inserter for a container • However, much of the work is done for you • Learning different combinations expands your intuition • Look for ways to explore and use additional combinations • Again, a key benefit is efficiency of effort • Write less code (and more importantly debug less code) • Build more sophisticated programs more easily