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

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

View Full Document Right Arrow Icon
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 inefficient! Iterators and iterator operations Lists as a different 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 first 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);
Background image of page 1

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

View Full Document Right Arrow Icon
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);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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 Right Arrow Icon
Ask a homework question - tutors are online