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

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Stacks, Queues, and Linked Lists
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
CSE Lecture 12 – Linked Lists …
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
More on the STL vector list stack queue priority_queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Data Structures Using C++ 2E
Writing Your Own STL Container Ray Lischner
Data Structures Using C++ 2E
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Lecture 7 : Intro. to STL (Standard Template Library)
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Cpt S 122 – Data Structures Abstract Data Types
Standard Template Library
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
Chapter 4 Linked Lists.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Introduction to Linked Lists
Object Oriented Programming COP3330 / CGS5409
A Sorted, Unique Key Container
C++ STL Vector Container
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Recitation Outline C++ STL associative containers Examples
Doubly Linked List Implementation
Recursive Linked List Operations
Dynamic Data Structures
A Robust Data Structure
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Doubly Linked List Implementation
The List Container and Iterators
A dictionary lookup mechanism
Presentation transcript:

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

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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 © 2004 - 2016 – Curt Hill

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