1 / 9

Combining STL Features

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.

haywood
Download Presentation

Combining STL Features

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. #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

  3. 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

  4. 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

  5. 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

  6. 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?

  7. 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

  8. 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?

  9. 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

More Related