Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.

Similar presentations


Presentation on theme: "CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties."— Presentation transcript:

1 CSE 332: Combining STL features 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 CSE 332: Combining STL features Think about the Kinds of Abstractions You Want #include 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

3 CSE 332: Combining STL features Let’s Say We Want to Square Some Numbers template struct square_functor : public unary_function { 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) = x 2 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

4 CSE 332: Combining STL features Applying Functors is Easy with Transform int int_array [] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; vector int_squares; transform (int_array, int_array + sizeof(int_array)/sizeof(int), back_inserter(int_squares), square_functor ()); copy (int_squares.begin(), int_squares.end(), ostream_iterator (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

5 CSE 332: Combining STL features 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 (cout, " "), square_functor ()); /* 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

6 CSE 332: Combining STL features What If We Want to Work With Another Type? vector fractions; back_insert_iterator > 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 (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

7 CSE 332: Combining STL features The Combinations Are All Appropriately Generic vector squared_fractions; transform (fractions.begin(), fractions.end(), back_inserter(squared_fractions), square_functor ()); copy (squared_fractions.begin(), squared_fractions.end(), ostream_iterator (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

8 CSE 332: Combining STL features What Else Can We Combine? transform (squared_fractions.begin(), squared_fractions.end(), ostream_iterator (cout, " "), sqrt); /* output: 1 2 3 4 5 6 7 8 9 */ transform (fractions.begin(), fractions.end(), ostream_iterator (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

9 CSE 332: Combining STL features 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


Download ppt "CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties."

Similar presentations


Ads by Google