##### Personal tools
You are here: Home Exercises (optional)
« September 2020 »
September
SuMoTuWeThFrSa
12345
6789101112
13141516171819
20212223242526
27282930

# Exercises (optional)

To exercise STL, Boost and CGAL

### STL & Functors

Taken from http://www.cset.oit.edu/~yangs/CST420/

1. Write a functor class template SumsTo that generates function objects taking two objects of the same type (x and y) and returning true iff x+y==z, where z is specified at the time the functor object is created. For example, SumsTo<int>(10)(x, y) should return true iff x+y==10. Test your template by using the two-sequence form of transform(): pass it two sequences and have it print out whether corresponding elements in the sequences sum to a user-specified value.
2. Use accumulate() to calculate the total length of all the strings in a set<string>. Be sure to use the correct return type for string::size().

### STL & Boost (a little more advanced then the previous)

1. Palindrome
1. Use boost::reverse_iterator together with STL's equal function to check if a given container is a palindrome.  The algorithm can do redundant work, but it should work for any container that has bidirectional iterators and comparable value_type.
2.  Check your code on both vector<int> and string.
3. Bonus: make the function a template function receiving an iterator range.
2. Indirect sort - We wish to sort an array of pointers (iterators) according to the pointees values. This is a very useful thing as the standard sort function copies the elements which can be quite expensive.
1. Write a predicate (functor returning bool) in the name of indirect_less that, using the regular sort algorithm, can be used to sort a sequence of pointers to integers according the integers' values. Check your function for some integer array.
2. Bonus: make the predicate a templated predicate to work for any iterator type. Use boost::counting_iterator together with copy algorithm to get a sequence of iterators to integers from a sequence of integers and use your predicate to sort the sequence. Print the result using boost::indirect_iterator, std::copy algorithm and std::ostream_iterator.
3. Bonus: change indirect_less to indirect_predicate. The new templated functor will be templated by the iterator type and by a functor that operates on the value_type. The functor returns the value of the given predicate on the pointees.