{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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.
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern