# 08_iterators - CSCI-1200 Data Structures Fall 2010 Lecture...

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

CSCI-1200 Data Structures — Fall 2010 Lecture 8 — Iterators Review from Lecture 7 Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors Templated classes Implementation of the DS Vec class, mimicking the STL vector class Today Erasing items from vectors is ineﬃcient! Iterators and iterator operations Lists as a diﬀerent sequential container class. Optional Reading: Ford & Topp Ch 6; 8.1 Another vector operation: pop back We have seen how push back adds a value to the end of a vector, increasing the size of the vector by 1. There is a corresponding function called pop back , which removes the last item in a vector, reducing the size by 1. There are also vector functions called front and back which denote (and thereby provide access to) the ﬁrst and last item in the vector, allowing them to be changed. For example: vector<int> a(5, 1); // a has 5 values, all 1 a.pop_back(); // a now has 4 values a.front() = 3; // equivalent to the statement, a[0] = 3; a.back() = -2; // equivalent to the statement, a[a.size()-1] = -2; 8.2 Example: Course Enrollment and Waiting List Today we’ll look at a program to build and maintain the class list and the waiting list for a single course. The program is structured to handle interactive input. Error checking ensures that the input is valid. Vectors store the enrolled students and the waiting students. The main work is done in the two functions enroll student and remove student . The invariant on the loop in the main function determines how these functions must behave. // Build and maintain a list of students enrolled in a class and the waiting list. #include <algorithm> #include <iostream> #include <string> #include <vector> #include <assert.h> using namespace std; vector<string>& enrolled, vector& waiting); vector<string>& enrolled, vector& waiting); void erase_from_vector(unsigned int i, vector<string>& v);

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

View Full Document
int main() { // Read in the maximum number of students in the course unsigned int max_students; cout << "\nWelcome to the enrollment program for CSCI 1200\n" << "Please enter the maximum number of students allowed\n"; cin >> max_students; // Initialize the vector vector<string> enrolled; vector<string> waiting; // Invariant: // (1) enrolled contains the students already in the course, // (2) waiting contains students who would will to be admitted (in // the order of request) if a spot opens up, in the // (3) enrolled.size() <= max_students, // (4) if the course is not filled (enrolled.size() != max_students) // then waiting is empty do { // check (part of) the invariant assert (enrolled.size() <= max_students); assert (enrolled.size() == max_students || waiting.size() == 0);
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

### Page1 / 6

08_iterators - CSCI-1200 Data Structures Fall 2010 Lecture...

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

View Full Document
Ask a homework question - tutors are online