1 Standard Containers: Lists Gordon College Resource:

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Beginning C++ Through Game Programming, Second Edition
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Stacked Deque Gordon College Stacked Decks - get it?
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
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.
Searching Arrays Linear search Binary search small arrays
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.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Generic Programming Using the C++ Standard Template Library.
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.
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
The Standard Template Library Container Classes Version 1.0.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Kruse/Ryba Ch071 Object Oriented Data Structures Searching STL Vector Sequential Search Binary Search Comparison Trees.
CS505 Data Structures and Algorithms
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
CSCI 3333 Data Structures Linked Lists
Doubly Linked List Implementation
Recursive Linked List Operations
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
Containers: Queue and List
Doubly Linked List Implementation
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
A dictionary lookup mechanism
Presentation transcript:

1 Standard Containers: Lists Gordon College Resource:

2 Lists Another sequential container - the elements have a specific position within the container Internal representation - next lecture Problems with Vector (dynamic arrays): 1.Insert/Deletion from middle of container - not efficient 2.Insert/Deletion from end of container - not efficient if this means expanding or deflating the size of the array (typically not viewed as much of a problem) Solution - the List

3 Lists Advantages to list containers: * Efficient insertion and removal of elements anywhere in the container (constant time). * Efficient moving elements and block of elements within the container or even between different containers (constant time). * Iterating over the elements in forward or reverse order (linear time). * Like Vector - can expand and deflate as needed perform generally better in inserting, extracting and moving elements in any position within the container

4 Lists Disadvantage to list containers: * lack direct access to elements by position (vector has direct access) Must linearly search for a matching element.

5 The list Container A type-independent pattern for an array class –capacity can expand –self contained Declaration template class list {... } ;

6 The list Container Constructors list first; // default - empty list of ints list second (4,100); // four ints with value 100 list third (second.begin(),second.end()); // iterating through second list fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; list fifth (myints, myints + sizeof(myints) / sizeof(int) );

7 The list Container Destructor ~list ( ); The object's destructor gets called automatically when the object leaves scope.

8 A quick word about types for(vector ::size_type i=0;i<strV.size();i++) cout << strV[i] << " "; cout << endl; Typically, we use int or unsigned types to deal with the index into a vector - however the “correct” way is to use a constant size_type associated with the class. Why? This makes our code work for all machine possibilities. Same case for all other containers.

9 list Operations Information about a vector's contents –size_type size() const; L.size(); –bool empty ( ) const; –size_type max_size () const; Basic access and add –reference back ( ); –reference front ( ); –void push_back ( const T& x ); –void push_front ( const T& x );

10 list Operations Remove - iterator erase ( iterator position ); - iterator erase ( iterator first, iterator last ); it1 = mylist.erase (it1); it1 = mylist.erase (it1,it2); Return: iterator pointing to the new location of the element that followed the last element erased -void clear ( ); All the elements are dropped: destructors are called - leaving container with size of 0. Remove by position

11 list Operations Remove - void pop_back ( ); - void pop_front ( ); Removes the elements from back (front) and reduces the size by 1. Remove by position - void resize ( size_type sz, T c = T() ); mylist.resize(5); mylist.resize(8,100); mylist.resize(12); If the sz is smaller than list then some of the elements are dropped (destructor called) Default constructor for type called 100 is used as fill

12 list Operations Remove - void remove ( const T& value ); Removes from the list all the elements with a specific value. This calls the destructor of these objects and reduces the list size by the amount of elements removed. Remove by value - void unique ( ); Removes all duplicate values - leaving only the first value

13 list Operations Add - iterator insert ( iterator position, const T& x ); - void insert ( iterator position, size_type n, const T& x ); - void insert ( iterator position, InputIterator first, InputIterator last ); mylist.insert (it,10); mylist.insert (it,2,20); vector myvector (2,30); mylist.insert (it,myvector.begin(),myvector.end());

14 list Operations void swap ( list & lst ); list first (3,100); list second (5,200); first.swap(second); void reverse ( ); for (int i=1; i<10; i++) mylist.push_back(i); mylist.reverse(); OUTPUT?

15 list Operations void splice ( iterator position, list & x ); // entire x is placed into list void splice ( iterator position, list & x, iterator i ); // only element pointed to by iterator is placed into list void splice ( iterator position, list & x, iterator first, iterator last ); // a range of elements from x is placed into list

16 sort list mylist; list ::iterator it; mylist.push_back ("one"); mylist.push_back ("two"); mylist.push_back ("Three"); mylist.sort(); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; mylist.sort(compare_nocase); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl;

17 sort list mylist; list ::iterator it; mylist.push_back ("one"); mylist.push_back ("two"); mylist.push_back ("Three"); mylist.sort(); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; mylist.sort(compare_nocase); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; bool compare_nocase (string first, string second) { unsigned int i=0; while ( (i<first.length()) && (i<second.length()) ) { if (tolower(first[i])<tolower(second[i])) return true; else if (tolower(first[i])>tolower(second[i])) return false; ++i; } if (first.length()<second.length()) return true; else return false; }

18 sort list mylist; list ::iterator it; mylist.push_back ("one"); mylist.push_back ("two"); mylist.push_back ("Three"); mylist.sort(); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; mylist.sort(compare_nocase); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl;

19 Predicate remove_if A Predicate is a Unary Function whose result represents the truth or falsehood of some condition. // a predicate implemented as a function: bool single_digit (const int& value) { return (value<10); } // a predicate implemented as a class: class is_odd { public: bool operator() (const int& value) {return (value%2)==1; } }; EXAMPLE mylist.remove_if (single_digit); mylist.remove_if (is_odd());

20 Predicate unique bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); } EXAMPLE mylist.unique (same_integral_part);

21 Contrast Lists and Vectors ListsVectors Constant time O(1) to add or remove elements from inside the list Element access is linear Constant time to move elements or blocks of elements within or between lists Linear time O(n) to add or remove elements from inside the list Element access is constant Linear time to move elements or blocks of elements within or between lists