lecture37-apr30

# lecture37-apr30 - Lecture 37 Announcements Exam 3 on Wed....

This preview shows pages 1–2. Sign up to view the full content.

1 Lecture 37 Announcements Exam 3 on Wed. –sample exam out Assignment 7 Q&A; due last day of classes Topics of the day • More STL– material not in the book • Iterators • Generic algorithms • Non sequence container types • Exceptions (time permitting) Iterators Smart pointer objects used to cycle through the contents of a container - declared iterator Five different kinds of iterators • Random access - store and retrieve values anywhere • Bidirectional - store/retrieve values in either forward or backward direction • Forward - store/retrieve values in forward direction only • Input - retrieve in forward direction • Output - store in forward direction Can increment, decrement and apply * For vector, e.g. • vector<int> v(10); // create a vector of 10 ints • vector<int> :: iterator p; // create an iterator named p // for int vectors • Use functions begin( ), end ( ) and iterator arithmetic to cycle through the values of v Output a Vector - Example 2 // output the elements of a vector of type T // using an iterator – acts like a smart pointer template <typename T> void outputVector(vector<T> &v) { vector <T> :: iterator p ; for(p = v.begin() ; p < v.end() ; p++ ) {cout << *p << “ ”;} cout << endl; } Some Useful Member Functions Common To All Containers Commonly used member functions are: push_back(), size(), begin(), end() iterator insert(iterator pos, const T& x) Inserts x before pos. iterator erase(iterator pos) Erases the element at position pos. void clear() Erases all of the elements. void resize(n, t = T()) Inserts objects of type T created using the default constructor or erases elements at the end to size n. bool empty() true if the container’s size is 0. void pop_front()/pop_back() Just removes the first/last element . STL's Algorithms STL has a collection of more than 80 generic algorithms • not member functions of STL's container classes • do not access containers directly They are stand-alone template functions • operate on data by means of iterators Designed to operate on a sequence of elements Designate a (sub)sequence by using two iterators • One positioned at the first element of the sequence • One positioned after the last element in the sequence Algorithms from <numeric> library contains template functions that operate on sequential containers - intended for use with numeric sequences (e.g. valarrays )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## lecture37-apr30 - Lecture 37 Announcements Exam 3 on Wed....

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online