09_lists - CSCI-1200 Data Structures - Fall 2010 Lecture 9...

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 9 — Lists Review from Lecture 8 We wrote several versions of a program to maintain a class enrollment list and an associated waiting list. The first version used vectors to store the information. Unfortunately, erasing items from vectors is inefficient. In the second version, we explored iterators and iterator operations as a different means of manipulating the contents of the vector. This allows us to replace the vector with a list in the third version. There is an erase function for both vectors and lists. The vector erase function does pretty much what we did in our enrollment example program. The list erase function is much more efficient (more on this next week!). For the enrollment problem, the list is a better sequential container class than the vector. Today’s Class Returning references to member variables from member functions Review of iterators and iterator operations STL Lists, erase and insert on lists Differences between indices and iterators, differences between lists and vectors Introductory example on linked lists. Basic linked list operations: Stepping through a list Push back 9.1 References and Return Values A reference is an alias for another variable. For example: string a = "Tommy"; string b = a; // a new string is created using the string copy constructor string& c = a; // c is an alias/reference to the string object a b[1] = ’i’; cout << a << " " << b << " " << c << endl; // outputs: Tommy Timmy Tommy c[1] = ’a’; cout << a << " " << b << " " << c << endl; // outputs: Tammy Timmy Tammy The reference variable c refers to the same string as variable a . Therefore, when we change c , we change a . Exactly the same thing occurs with reference parameters to functions and the return values of functions. Let’s look at the Student class from Lecture 4 again: class Student { public: const string& first_name() const { return first_name_; } const string& last_name() const { return last_name_; } // etc. ... private: string first_name_; string last_name_; // etc. .. };
Background image of page 1

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

View Full DocumentRight Arrow Icon
In the main function we had a vector of students: vector<Student> students; Based on our discussion of references above and looking at the class declaration, what if we wrote the following. Would the code then be changing the internal contents of the i-th Student object? string & fname = students[i].first_name(); fname[1] = ’i’ The answer is NO! The Student class member function first_name returns a const reference. The compiler will complain that the above code is attempting to assign a const reference to a non-const reference variable.
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.

Page1 / 6

09_lists - CSCI-1200 Data Structures - Fall 2010 Lecture 9...

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