Presentation is loading. Please wait.

Presentation is loading. Please wait.

18 – Sequential Containers

Similar presentations


Presentation on theme: "18 – Sequential Containers"— Presentation transcript:

1 18 – Sequential Containers
4.7 Implementation of a Double- Linked List Class 4.8 Application of the list Class 4.9 Standard Library Containers 4.10 Standard Library Algorithms and Function Objects 18 – Sequential Containers

2 Attendance Quiz #17 Sequential Containers

3 Tip #18: Improve Success Ratio
Sequential Containers Do the tough stuff first. Think more, code less. For big stuff, get someone’s brain wrapped around it. Under promise, over deliver. When something breaks, look at your assumptions. Before you begin coding, decide how you are going to test it. Trust your team, but ask to see. Learn how to listen. Don’t whine! (ie. Clear the Bozo bit.) No Coding after midnight!

4 4.8, pgs. 285-286 4.8 Application of the list Class
Case Study: Maintaining an Ordered List 4.8, pgs

5 Application of the list Class
Sequential Containers Problem We want to maintain this list so that it will be in alphabetical order even after students have added and dropped the course. Analysis Develop a general Ordered_List class that can be used to store any group of objects that can be compared using the less-than operator. Solution Write a new Ordered_List class. Inherit the C++ list class to create a new Ordered_List class and over-ride list functions (not recommended.) Or create a new Ordered_List class that uses a C++ list to store the items. Use list functions where appropriate (delegation). Write new function where needed.

6 Ordered Linked List Insertion
Sequential Containers Algorithm for Insertion Find the first item in the list that is greater than or equal to the item to be inserted Create an iterator that starts at the beginning of the list while the iterator is not at the end and the item at the iterator position is less than the item to be inserted Advance the iterator Insert the new item before this one.

7 Delegation Code for Insertion Use Delegation for the Other Functions
Sequential Containers Code for Insertion Inside a declaration or a definition of a template, typename can be used to declare that a dependent name is a type. void insert(const Item_Type& an_item) { typename std::list<Item_Type>::iterator itr = a_list.begin(); while (itr != a_list.end() && *itr < an_item) ++itr; // itr points to the first item >= an_item or the end a_list.insert(itr, an_item); } Use Delegation for the Other Functions void remove(const Item_Type& item) { a_list.remove(item); } A delegate is a class or method that wraps a pointer or reference to an object instance, a member method of that object's class to be called on that object instance, and provides a method to trigger that call.

8 4.9, pgs. 292-295 4.9 Standard Library Containers
Common Features of Containers Sequences Associative Containers Vector Implementation Revisited 4.9, pgs

9 Standard Library Containers
Sequential Containers The C++ standard uses the term container to represent a class that can contain objects. The term concept is used to represent the set of common requirements for a container: C++ defines a common interface for all containers and then splits the set of containers into sequential containers and associative containers. Common requirements unique to these subsets are also defined and the individual containers have their own additional requirements. Although a list and a vector have several member functions in common, they are not polymorphically, but can be used interchangeably by generic algorithms. In a sequential container, at any given time each element has a particular position relative to the other items in the container. items in a sequence (ie., a vector or a list) follow some linear arrangement. In an associative container, on the other hand, there is no particular position for each item in the container. An item is accessed by its value, rather than by its position.

10 4.10 Standard Library Algorithms and Function Objects
The find Function The Algorithm Library The swap Function Function Objects 4.10, pgs

11 The Algorithm Library Sequential Containers The standard library contains many useful template functions defined in the header <algorithm>, that use a pair of iterators to define the sequence of input values. Some of them also take function objects as parameters The template functions perform fairly standard operations on containers, such as Applying the same function to each element (for_each) Copying values from one container to another (copy) Searching a container for a target value (find, find_if) Sorting a container (sort) Summing container values (accumulate) Apply function to container objects (transform) This fragment copies the contents of a_list into a_vector: a_vector.resize(a_list.size()); copy(a_list.begin(), a_list.end(), a_vector.begin());

12 template<typename II, typename F>
Function Behavior template<typename II, typename F> F for_each(II first, II last, F fun) Applies the function fun to each object in the sequence. The function fun is not supposed to modify its argument. The iterator argument II is required to be an input iterator. This means that the sequence is traversed only once. template<typename II, typename T> II find(II first, II last, T target) Finds the first occurrence of target in the sequence. If not found, last is returned. template<typename II, typename P> II find_if(II first, II last, P pred) Finds the first occurrence of an item in the sequence for which function pred returns true. If not found, last is returned. template<typename FI> FI min_element(FI first, FI last) FI max_element(FI first, FI last) Finds the min/max element in the sequence FI; first..last. FI is a forward iterator. template<typename II, typename OI> OI copy(II first, II last, OI result) Copies the sequence first..last into result..(result + (last - first)). II is an input iterator, and OI is an output iterator. template<typename II, typename OI, typename OP> OI transform(II first, II last, OI result, OP op) Applies op to each element of the sequence first..last and places the result in result..(result + (last - first)). template<typename II1, typename II2, typename OI, typename BOP> OI transform(II1 first1, II1 last1, II2 first2, OI result, BOP bin_op) Applies bin_op to each pair of elements of the sequences first1..last1 and first2..(first2 + (last1 - first1)) and places the result in result..(result + (last1 - first1))

13 template<typename T> void swap(T& a, T& b)
Function Behavior template<typename T> void swap(T& a, T& b) Exchanges the contents of a and b. template<typename BI> void reverse(BI first, BI last) Reverses the sequence first..last. BI is a bidirectional iterator. template<typename RI> void random_shuffle(RI first, RI last) Randomly rearranges the contents of first..last. RI is a random-access iterator. void sort(RI first, RI last) Sorts the contents of first..last based on the less-than operator applied to pairs of elements. template<typename RI, typename COMP> void sort(RI first, RI last, COMP comp) Sorts the contents of first..last based on the binary operator COMP (a function operator). COMP is a function class that takes two arguments and returns a bool. template<typename II, typename T> T accumulate(II first, II last, T init) Computes init plus the sum of the elements in first..last. Note that this function is defined in the header <numeric>. template<typename II1, typename II2> bool equal(II1 first1, II1 last1, II2 first2) template<typename II1, typename II2, typename BP> bool equal(II1 first1, II1 last2, II2 first2, BP pred) Compares each element in the sequence first1..last1 to the corresponding elements in the sequence first2..first2 + (last1 - first1). If they are all equal, then this returns true. The second form uses the function object pred to perform the comparison.

14 Function Objects

15 Data field divisor stores the number we want to divide by
Function Objects Sequential Containers The function call operator (operator()) can be overloaded by a class. A class that overloads this operator is called a function class and an object of such a class is called a function object (or functor.) As an example, we may want to find a value divisible by another value. We can create a function class Divisible_By whose constructor takes the divisor as an argument: Data field divisor stores the number we want to divide by class Divisible_By { private: int divisor; public: Divisible_By(int d) : divisor(d) {} bool operator()(int x) return x % divisor == 0; } }; The definition of operator() tests the remainder resulting from the division of the function argument (int x) by the value of divisor The expression Divisible_By(3) creates a function object that returns true if the argument passed to it is divisible by 3 The expression Divisible_By(5) creates a function object that tests for integers divisible by 5

16 Function Objects Sequential Containers template<typename II, typename P> II find_if(II first, II last, P pred) Finds the first occurrence of an item in the sequence for which function pred returns true. If not found, last is returned. Template parameter II is a placeholder for an input iterator; template parameter P is a placeholder for a function class with an operator() that returns a bool value. // Find first number divisible by 3 in list_1. list<int>::iterator = iter; iter = find_if(list_1.begin(), list_1.end(), Divisible_By(3)); if (iter != list_1.end()) cout << "The first number divisible by 3 is " << *iter << endl; else cout << "There are no numbers divisible by 3" <<endl; // Find first number divisible by 5 in list_1. iter = find_if(list_1.begin(), list_1.end(), Divisible_By(5)); if (iter != list_1.end()) cout << "The first number divisible by 5 is " << *iter << endl; else cout << "There are no numbers divisible by 5" << endl;

17 Write a template Functor named Matcher such that function calls to a Matcher object returns true if same, else false. // Define templated Matcher object. // Instantiate Matcher object isMatch. // Use isMatch to test if the number n is 5. template <typename T> class Matcher { private: T target; public: Matcher(T m) : target(m) {} bool operator()(T x) { return x == target; } }; Matcher<int> isMatch(5); if (isMatch(n)) cout << n << " == 5" << endl; else cout << n << " != 5" << endl;

18


Download ppt "18 – Sequential Containers"

Similar presentations


Ads by Google