ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,

Slides:



Advertisements
Similar presentations
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Advertisements

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Algorithm design techniques
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Writing Your Own STL Container Ray Lischner
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
Data Structures Using C++ 2E
Containers Overview and Class Vector
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
C++ Review STL CONTAINERS.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
Outline This topic discusses the insertion sort We will discuss:
What remains Topics Assignments Final exam
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Open addressing.
Lists - I The List ADT.
Lists - I The List ADT.
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
A dictionary lookup mechanism
Algorithms and templates
Standard Template Library
Presentation transcript:

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved. Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved The Standard Template Library (STL)

2 Standard Template Library Outline In this topic, we will look at linked lists –The Node and List classes –Accessors and mutators –The implementation of various member functions –Stepping through a linked list –Defining the copy and assignment operator –Defining move constructors and move assignment operators –Discussed efficiencies

3 Standard Template Library Arrays The Standard Template Library has three variations on arrays: template class array; template > class vector; template class bitset;

4 Standard Template Library array This is a sequence container with a linear order –Elements are accessed by their position The memory allocation is contiguous –Random access is  (1) The memory is allocated at compile time

5 Standard Template Library array To make return types more standard, the C++ STL defines specific member types associated with each class: array ::value_typeT array ::referenceT & array ::const_referenceT const & array ::pointerT * array ::const_pointerT const * array ::iterator array ::const_iterator array ::reverse_iterator array ::const_reverse_iterator array ::size_typesize_t array ::difference_type ptrdiff_t

6 Standard Template Library array Member functions include: –The eight iterators begin end rbegin rend cbegin cend crbegin crend iterator begin() noexcept; const_iterator begin() const noexcept; const_iterator cbegin() const noexcept; –Capacity constexpr size_type size() noexcept; constexpr size_type max_size() noexcept; constexpr bool empty() noexcept;

7 Standard Template Library array Member functions include: –Element access reference operator[]( size_type ); const_reference operator[]( size_type ) const; reference at( size_type ); const_reference at ( size_type ) const; reference front(); const_reference front() const; reference back(); const_reference back() const; pointer data() noexcept; const_pointer data() const noexcept;

8 Standard Template Library array Member functions include: –Modifiers void fill( const_reference ); void swap( array & ) noexcept(... );

9 Standard Template Library array Example: #include int main() { std::array v; for ( int i = 0; i < 5; ++i ) { v[i] = i; } for ( auto itr = v.begin(); itr != v.end(); ++itr ) { *itr = (*itr)^2; } v.fill( 7 ); int *ptr = v.data(); return 0; }

10 Standard Template Library bitset

11 Standard Template Library vector This is a sequence container with a linear order –Elements are accessed by their position The memory allocation is contiguous –Random access is  (1) The array allocation is dynamic –The size of the array can change at runtime The user can specify the method of allocation

12 Standard Template Library vector To make return types more standard, the C++ STL defines specific member types associated with each class: vector ::value_typeT vector ::referenceT & vector ::const_referenceT const & vector ::pointerT * vector ::const_pointerT const * vector ::iterator vector ::const_iterator vector ::reverse_iterator vector ::const_reverse_iterator vector ::allocator_typeallocate vector ::size_typesize_t vector ::difference_typeptrdiff_t

13 Standard Template Library vector Member functions include: –Constructors explicit vector(); explicit vector( size_type ); vector( size_type, const_reference ); template vector( InputIterator first, InputIterator last ); vector( vector const & ); vector( vector && ); vector( initializer_list );

14 Standard Template Library vector Member functions include: –Assignment operator vector &operator=( vector const & ); vector &operator=( vector && ); vector &operator=( initializer_list ); –The last lets us: std::vector v(10); v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

15 Standard Template Library vector Member functions include: –The eight iterators begin end rbegin rend cbegin cend crbegin crend –Each has the various signatures: iterator begin() noexcept; const_iterator begin() const noexcept; const_iterator cbegin() const noexcept;

16 Standard Template Library vector Member functions include: –Capacity size_type size() const noexcept; size_type capacity() const noexcept; size_type maxsize() const noexcept; void resize( size_type ); void resize( size_type, const_reference ); bool empty() const noexcept; void reserve( size_type ); void shrink_to_fit();

17 Standard Template Library vector Member functions include: –Element access reference operator[]( size_type ); const_reference operator[]( size_type ) const; reference at( size_type ); const_reference at ( size_type ) const; reference front(); const_reference front() const; reference back(); const_reference back() const; pointer data() noexcept; const_pointer data() const noexcept;

18 Standard Template Library vector Member functions include: –Modifiers template void assign( Iterator, Iterator ); void assign( size_type, const_reference ); void assign( initializer_list ); void push_back( const_reference ); void push_back( value_type&& ); void pop_back();

19 Standard Template Library vector Member functions include: –Modifiers iterator insert( const_iterator position, const_reference ); iterator insert( const_iterator position, size_type n, const_reference ); template iterator insert( const_iterator position, Iterator first, Iterator last ); iterator insert( const_iterator position, value_type&& ); iterator insert( const_iterator position, initializer_list );

20 Standard Template Library vector Member functions include: –Allocator allocator_type get_allocator() const noexcept; –Non-member function overloads template void swap( vector &, vector & ); template bool operator==( const vector &, const vector & ); Includes the relational operators !=,, and >= Uses a lexicographical comparison

21 Standard Template Library vector One specialization of vector is for Boolean values: –Normally, each bool occupies one byte –Reasonable specializations of vector use one bit per entry –One new function: void flip() noexcept; –A mechanism for referencing individual bits and interpreting them as type bool

22 Standard Template Library vector One thing that has been overlooked is: how is memory allocated? By default, memory allocation is performed using new[] and delete[] –What if this is too slow or inappropriate for a particular use of vector? The actual class definition is: template > class vector;

23 Standard Template Library vector An allocator class must have specific member types and functions: template class Allocator { public: typedef T value_type; typedef T * pointer; typedef const T * const_pointer; typedef T & reference; typedef const T & const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef propagate_on_container_move_assignment true_type; template struct rebind { typedef Allocator other; };

24 Standard Template Library vector allocator() nothrow; allocator ( const allocator & ) nothrow; template allocator( const allocator & ) nothrow; ~allocator() throw; pointer address( reference ) const noexcept; const_pointer address( const_reference ) const noexcept; pointer allocate( size_type, allocator ::const_pointer = 0 ); void deallocate( pointer, size_type ); size_type max_size() const nothrow; template void construct( U* p, Args&&... args ); template void destroy ( U* p ); };

25 Standard Template Library vector Why would you want a different allocator? –Suppose you want persistent memory allocation—allocation that continues from one execution of a program to the next –Intel’s thread building blocks improve the performance of multithreaded applications by using std::vector > –Electronic Arts has a STL optimized for gaming software—memory tends to be more restrictive on gaming platforms –Tracking allocations and deallocations for debugging or efficiency –Suppose you want to use memory-mapped files—addresses in memory that are mapped not to RAM but to virtual memory From the point of view of portability, all the machine- specific things which relate to the notion of address, pointer, and so on, are encapsulated within a tiny, well- understood mechanism. Alex Stepanov, designer of the STL

26 Standard Template Library Linked Lists The Standard Template Library has two variations on a linked list: template > class list; template > class forward_list;

27 Standard Template Library Stacks, Queues, and Deques The Standard Template Library has all three classes: template > class deque; template > class stack; template > class queue;

28 Standard Template Library Weakly Ordered Containers Four containers are based on weak linear orderings: template < typename Key, class Compare = less, class Alloc = allocator > class set; template < typename Key, class Compare = less, class Alloc = allocator > class multiset;

29 Standard Template Library Weakly Ordered Containers Four containers are based on weak linear orderings: template < typename Key, typename T, class Compare = less, class Alloc = allocator > > class map; template < typename Key, typename T, class Compare = less, class Alloc = allocator > > class multimap;

30 Standard Template Library Weakly Ordered Containers What’s the difference? –A simple container stores objects –An associative containers stores an object related to a key were accesses are performed using the key –A weak ordering is a linear ordering of equivalence classes With linear orderings, either a b With weak orderings, either a b That is, if a is neither less than or greater than b, it is equivalent to b Example: people compared using their age in years –The container may store either Only a single item per equivalence class, or Multiple items per equivalence class

31 Standard Template Library Weakly Ordered Containers Which are which? The class definitions: –The class definitions for set and multiset are the same – map and multimap are similar with: Two additional member functions for access via the keys Arguments for searching are based on keys Returns are based on what is being associated with the key Items per equivalence classSimple ContainerAssociative Container At most one set map An arbitrary number multiset multimap

32 Standard Template Library set To make return types more standard, the C++ STL defines specific member types associated with each class: set ::key_typeKey set ::value_typeKey set ::referenceKey & set ::const_referenceKey const & set ::pointerKey * set ::const_pointerKey const * set ::iterator set ::const_iterator set ::reverse_iterator set ::const_reverse_iterator set ::size_typesize_t set ::difference_typeptrdiff_t

33 Standard Template Library Priority Queues The Standard Template Library has a priority queue classes: template < typename T, class Container = vector, class Compare = less > class priority_queue;

34 Standard Template Library Hashed Containers For containers are based on hashing: template < typename Key, class Hash = hash, class Pred = equal_to, class Alloc = allocator > class unordered_set; template < typename Key, class Hash = hash, class Pred = equal_to, class Alloc = allocator > class unordered_multiset;

35 Standard Template Library Hashed Containers For containers are based on hashing: template < typename Key, typename T, class Hash = hash, class Pred = equal_to, class Alloc = allocator > > class unordered_set; template < typename Key, typename T, class Hash = hash, class Pred = equal_to, class Alloc = allocator > > class unordered_multiset;

36 Standard Template Library unordered_set This is a simple container with unordered elements –Random access is  (1) The elements stored are unique The user can specify the method of allocation

37 Standard Template Library unordered_set To make return types more standard, the C++ STL defines specific member types associated with each class: key_typeKey value_typeKey hasherhash key_equalequal_to referenceKey & const_referenceKey const & pointerKey * const_pointerKey const * iterator const_iterator local_iterator const_local_iterator allocator_typeallocate size_typesize_t difference_typeptrdiff_t

38 Standard Template Library unordered_set Member functions include: –Constructors explicit unordered_set( size_type, const hasher & = hasher(), const key_equal & = key_equal(), const allocator_type & = allocator_type() ); unordered_set( unordered_set const & ); unordered_set( unordered_set && ); template unordered_set( InputIterator first, InputInterator last,... ); unordered_set( initializer_list,... );

39 Standard Template Library unordered_set Member functions include: –Assignment operator unordered_set &operator=( unordered_set const & ); unordered_set &operator=( unordered_set && ); unordered_set &operator=( initializer_list );

40 Standard Template Library unordered_set Member functions include: –The four forward iterators begin end cbegin cend –Each has the various signatures: iterator begin() noexcept; const_iterator begin() const noexcept; local_iterator begin( size_type ); const_local_iterator begin( size_type ) const;

41 Standard Template Library unordered_set Member functions include: –Capacity size_type size() const noexcept; size_type maxsize() const noexcept; bool empty() const noexcept;

42 Standard Template Library unordered_set Member functions include: –Element lookup iterator find( const key_type & ); const_iterator find( const key_type & ) const; size_type count( const key_type & ); pair equal_range( const key_type & ); pair equal_range( const key_type & ) const;

43 Standard Template Library unordered_set Member functions include: –Modifiers template iterator emplace( Args&&... ); template iterator emplace_hint( const_iterator, Args&&... ); pair insert( reference& ); pair insert( value_type && ); pair insert( const_iterator, reference& ); pair insert( const_iterator, value_type && );

44 Standard Template Library unordered_set Member functions include: –Modifiers iterator erase( const_iterator position ); iterator erase( reference ); iterator insert( const_iterator, const_iterator ); void clear() noexcept; void swap( unordered_set & );

45 Standard Template Library unordered_set Member functions include: –Allocator allocator_type get_allocator() const noexcept; –Non-member function overloads template void swap( vector &, vector & ); template bool operator==( const vector &, const vector & ); Includes the relational operators !=,, and >= Uses a lexicographical comparison

46 Standard Template Library Summary Data Structure STL Containers Array array bitset vector Linked lists forward_list list Stacks, etc. stack queue deque Weakly ordered set multiset map multimap Priority queue propority_queue Hash tables unordered_set unordered_mulitset unordered_map unordered_mulitmap

47 Standard Template Library Summary We have looked at all the containers implemented in the STL –These cover all data structures looked at in this class –The most recent additions were singly linked lists and hash tables

48 Standard Template Library References Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, 2 nd Ed., Addison Wesley, 1998, §5.4, pp Wikipedia, These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.