Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 121 – Spring 2016 Professor J. Michael Moore

Similar presentations


Presentation on theme: "CSCE 121 – Spring 2016 Professor J. Michael Moore"— Presentation transcript:

1 CSCE 121 – Spring 2016 Professor J. Michael Moore
STL: Vector vs. List CSCE 121 – Spring 2016 Professor J. Michael Moore Based on slides created by Bjarne Stroustrup and Jennifer Welch

2 Vector and List Implementations
Let’s peek inside vector and list implementation focusing on iterators and more functions Topics: what type is a vector/list iterator? begin and end functions erase function (remove an element) insert function (add a function anywhere)

3 vector Implementation
template<class T> class vector { T* elements; // ... using iterator = ? ? ? ; // type of an iterator is implementation-defined // and it varies; a vector iterator could be a // pointer to an element. “using” is the same as // typedef – gives convenient name to the type iterator begin(); // points to first element iterator end(); // points to one past last element iterator erase(iterator p); // remove element pointed to by p iterator insert(iterator p, const T& v); // insert new element v before element pointed to by p };

4 insert() into vector Shift down all the array elements after the indicated location and then put new element in that location Warning! If you have other iterators that refer to elements of the vector, they are now invalid some, and perhaps all, of the elements have moved so their addresses have changed

5 vector insert() Example
vector<int>::iterator p = v.begin(); ++p; ++p; ++p; vector<int>::iterator q = p; ++q; p = v.insert(p, 99); v: 6 p: q: 1 2 3 4 5 7 2 1 99 3 4 v: p: 5 q:

6 erase() from vector Shift up all the array elements starting at the indicated location to close up the “hole” Warning! If you have other iterators that refer to elements of the vector, they are now invalid some, and perhaps all, of the elements have moved so their addresses have changed

7 vector erase() Example
7 2 1 99 3 4 v: p: 5 q: p = v.erase(p); 6 2 1 3 4 5 v: p: q:

8 list Implementation template<class T> class list {
Link* elements; // ... using iterator = ? ? ? ; // type of an iterator is implementation-defined // and it varies; a list iterator could be a // pointer to a link node. “using” is the same as // typedef – gives convenient name to the type iterator begin(); // points to first element iterator end(); // points to one past last element iterator erase(iterator p); // remove element pointed to by p iterator insert(iterator p, const T& v); // insert new element v before element pointed to by p }; T value Link* prev Link* succ Link:

9 insert() and erase() for list
Since we have a linked list, we can insert and erase from the list without disturbing the locations of the other elements Manipulate the pointers in the Link nodes appropriately to splice in or out the relevant Link node Other iterators that point to list elements are NOT invalidated for the list container

10 list insert() Example 6 2 1 3 4 5 v: p: q:
2 1 3 4 5 v: p: q: list<int>::iterator p = v.begin(); ++p; ++p; ++p; list<int>::iterator q = p; ++q; v = v.insert(p, 99); 7 2 1 3 4 5 v: p: 99 q:

11 list insert() Example 7 2 1 3 4 5 v: p: 99 q: p = v.erase(p); 6 2 1 3
2 1 3 4 5 v: p: 99 q: p = v.erase(p); 6 2 1 3 4 5 v: p: q:

12 Acknowledgements Slides are based on those from and as modified by Jennifer Welch: Programming Principles and Practice Using C++, Second Edition, Bjarne Stroustrup, Pearson, :


Download ppt "CSCE 121 – Spring 2016 Professor J. Michael Moore"

Similar presentations


Ads by Google