



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Fall 2008;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




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.
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... };
vector
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’
const string & fname = students[i].first_name(); fname[1] = ’i’
array/vector:
7 5 8 1 9 7 5 8 1 9 0 1 2 3 4
list:
vector
v_itr = vec.begin() + i; // i-th location in a vector l_itr = lst.begin(); // first entry in a list s_itr = str.begin(); // first char of a string
*v_itr = 3.14; cout << *s_itr << endl; *l_itr = "Hello";
list
?
7 5 8 1 9
7 8 1 9
p q
p
list
p = v.erase(p);
Write a function that takes a list of integers, lst, and an integer, x. The function should 1) remove all negative numbers from the list, 2) verify that the remaining elements in the list are sorted in increasing order, and 3) insert x into the list such that the order is maintained.
#include
template
void main() { Node
Node
// set ll’s ptr member variable to // point to the same thing as variable q ll->ptr = q;
cout << "1st value: " << ll->value << "\n" << "2nd value: " << ll->ptr->value << endl; }
6
ll
value ptr
value ptr
q
NULL
8