Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2004 - 2016 – Curt Hill STL List Details Copyright © 2004 - 2016 – Curt Hill.

Similar presentations


Presentation on theme: "Copyright © 2004 - 2016 – Curt Hill STL List Details Copyright © 2004 - 2016 – Curt Hill."— Presentation transcript:

1 Copyright © 2004 - 2016 – Curt Hill
STL List Details Copyright © – Curt Hill

2 Copyright © 2004 - 2016 – Curt Hill
Forward This will have something in common with the vector It is another container class The design of the STL is to keep things similar All STL container classes are black boxes We do not need to know how it is organized to use We do need to understand the implementation to evaluate the tradeoffs Internally list is a doubly linked list This allows our iterators to go forward or backward Copyright © – Curt Hill

3 Copyright © 2004 - 2016 – Curt Hill
List Library Like a vector either <List.h> or <list> works If you use <list> You must also use using namespace std; This is preferred for recent versions of our IDE Copyright © – Curt Hill

4 Copyright © 2004 - 2016 – Curt Hill
Constructors Default: list<type> lt; This form requires a default constructor for type All primitives have a null constructor The default parameter is a alternative memory manager The sized one exists: list<type> ls(int N); Not needed, since list is not contiguous storage Makes it easy to convert one container class to another Copyright © – Curt Hill

5 Copyright © 2004 - 2016 – Curt Hill
Copy Constructors Copy: list<type> lc(container) The container is another STL list of same type Partial copy: list <type> vc(It1,It2) It1 and It2 are input iterators in an existing container class Recall that iterators are specialized pointers into a STL container class Does not have to be a list, but sequentiality has to exist This constructor then copies from It1 to It2 into a new list It1 better be before It2 This could be the entire container Copyright © – Curt Hill

6 Copyright © 2004 - 2016 – Curt Hill
Operators Assignment May use the = operator Erases the entire list and copies in the new one assign(It1,It2) Similar to partial copy constructor Reference The subscripting brackets may not be used with lists nor can the at function All such references need an iterator Copyright © – Curt Hill

7 Copyright © 2004 - 2016 – Curt Hill
Deep or Shallow Copy? Two terms pertaining to the copy of a pointer based structure A shallow copy only copies a pointer A deep copy copies the things referred to by the pointer and makes a new pointer What does STL do with assignments or partial copies? Deep and shallow Copyright © – Curt Hill

8 Copyright © 2004 - 2016 – Curt Hill
STL Copies What happens when one container is assigned to another or a partial copy constructor is executed? STL does a deep copy of all that it knows about It will use the copy constructor (or assignment operator) for each contained item If the copy constructor does a deep copy of the contained class then a deep copy occurs Otherwise a mixture of deep and shallow Copyright © – Curt Hill

9 Copyright © 2004 - 2016 – Curt Hill
Iterators List iterators are similar to other container classes Iterator presentation Copyright © – Curt Hill

10 Other member functions
void clear() Deletes all elements int size() Returns the number of elements int capacity() is not supported capacity() – size() is used in a vector to determine how many may be added without copying int max_size() Maximum capacity This is a function of available memory Copyright © – Curt Hill

11 Copyright © 2004 - 2016 – Curt Hill
More member functions void push_back(TYPE) Inserts a new item at end void pop_back() Deletes the last item void push_front(TYPE) Inserts a new item at front end This (and next) was not available on vector void pop_front() Deletes the first item Copyright © – Curt Hill

12 Insertion and deletion
iterator insert(iterator N, TYPE t) Insert value t before position N Returned iterator points at the new t In a vector this is hard, everything must be slid down In a list it is just pointer manipulation There are several other inserts as well iterator erase(interator M, iterator N) Deletes a range of elements from M to before N iterator erase(interator M) Delete the first one on the list that matches void remove(const TYPE &) Delete all that match based on comparisons Copyright © – Curt Hill

13 Copyright © 2004 - 2016 – Curt Hill
Comparisons Lists have the following operators overloaded: ==, !=, <, >, >=, <= These force an element by element comparison The < is true if every element is less than corresponding element in other Copyright © – Curt Hill

14 Copyright © 2004 - 2016 – Curt Hill
Other methods front() Returns reference to first element Same as end() if list is empty back Returns reference to last element swap (list<TYPE>) Swaps two lists of same type empty Returns size == 0 Copyright © – Curt Hill

15 Copyright © 2004 - 2016 – Curt Hill
List algorithms None of these are present in vector void reverse() Reverses the direction of a linked list void sort() Sorts the list Uses entire data as key void unique() Eliminates consecutive duplicate entries from the list, leaving only the first If the list is unordered there may be duplicates void splice(iterator position, list<T> first, list <T> last) Inserts into the list before position the sub-list starting at first and ending at last The default for last is the end of the list Copyright © – Curt Hill

16 Copyright © 2004 - 2016 – Curt Hill
New Include If you use: sort, unique, splice, find, reverse You will need: #include <algorithm> Copyright © – Curt Hill


Download ppt "Copyright © 2004 - 2016 – Curt Hill STL List Details Copyright © 2004 - 2016 – Curt Hill."

Similar presentations


Ads by Google