A Sorted, Unique Key Container

Slides:



Advertisements
Similar presentations
Chapter 9 Imperative and object-oriented languages 1.
Advertisements

The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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++1 Chapter 13 Standard Template Library (STL) II.
Data Structures Using C++ 2E
Writing Your Own STL Container Ray Lischner
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
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
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Object-Oriented Programming (OOP) Lecture No. 41
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
C++ Programming:. Program Design Including
Distinguishing logic from data type
Vectors Holds a set of elements, like an array
C++ Standard Library.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Basic Data Structures.
Searching.
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Chapter 9 – Sets and Maps 9.1 Associative Container
Introduction to Linked Lists
Basic Data Structures.
18 – Sequential Containers
Chapter 22: Standard Template Library (STL)
Object Oriented Programming COP3330 / CGS5409
Associative Structures
CS212: Object Oriented Analysis and Design
Arrays in Java What, why and how Copyright Curt Hill.
The Standard Template Library
Recitation Outline C++ STL associative containers Examples
Dynamic Data Structures
A Robust Data Structure
Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Standard Template Library
An Introduction to STL.
The IF Revisited A few more things Copyright © Curt Hill.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 9 – Sets and Maps 9.1 Associative Container
A dictionary lookup mechanism
Presentation transcript:

A Sorted, Unique Key Container STL Sets A Sorted, Unique Key Container Copyright © 2006-2017 Curt Hill

General characteristics Library is <set> or <set.h> Sorted, thus type must suitable for > or < or provide a compare object Maintains a total ordering, so you cannot change a key You must reinsert, that is delete and add Copyright © 2006-2017 Curt Hill

Declaration The template has three parameters Last two have default values set<T,COMPARE<T>,ALLOCATOR<T> > Compare is a binary function object that returns a boolean Allocator will do memory management Copyright © 2006-2017 Curt Hill

Constructors The default constructor creates the empty set A two iterator constructor creates a set with everything between the two iterators These iterators only have to be from a container that allows forward iteration A copy constructor also exists Copyright © 2006-2017 Curt Hill

Operations Assignment Comparisons Not subset at all Makes as a deep a copy as the contained class supports Comparisons Typical unusual STL compare s1 < s2 only if corresponding members are less Thus: {0, 1} <={0,2} but not {0,3} <={0,2,3} Not subset at all Copyright © 2006-2017 Curt Hill

Member functions void clear() int size() int max_size() Deletes all elements int size() Set cardinality int max_size() Maximum capacity This is a function of available memory empty() is true if the empty set Copyright © 2006-2017 Curt Hill

Other member functions iterator find(const TYPE&x) Returns the iterator or end() if(st.find(x)!=st.end()) is the same as xst iterator erase(interator N) Deletes an element, corresponds to insert Copyright © 2006-2017 Curt Hill

Insert insert(const TYPE & n) If successful Otherwise Inserts n into the set It returns a pair of items bundled together If successful The first is an iterator to the item The second is Boolean true Otherwise The first is the end iterator and false Pair is a template struct Used for bundling two things that need to be a function result Used here and other places (like maps) Copyright © 2006-2017 Curt Hill

Other Inserts iterator insert(iterator N, TYPE t) Insert value t before position N Notice that N is an iterator and not an integer The N iterator is a hint If the hint is accurate the insertion occurs in O(C) time otherwise in O(n) time Returned iterator points at the new t iterator insert(iterator first, iterator second) Inserts a sequence of items From another or same container class Copyright © 2006-2017 Curt Hill

Bounds Two methods that set an iterator Used for finding the closest iterator lower_bound(const TYPE & x) Returns an iterator to first element that is lesser or equal than x Returns the end if none exists iterator upper_bound(const TYPE & x) Returns an iterator to first element that is greater or equal than x Copyright © 2006-2017 Curt Hill

Algorithms Most of the set algorithms are not unique to the set These include: Union Intersection Difference These may require an additional include <algorithm> They may be applied to any sorted sequential containers other than set Copyright © 2006-2017 Curt Hill

Union OutputIterator set_union (InputIterator,InputIterator, InputIterator,InputIterator, OutputIterator) The first two iterators are the beginning and end of the first set, the second pair the second set and the OutputIterator is where they go The output iterator needs to be in a container suitable for output iterators Set does not allow outputiterators Copyright © 2006-2017 Curt Hill

Example // s1 = s1  s2 // All are vectors vector<int> s3; insert_iterator<set<int> > sout(s3,s3.begin()); set_union(s1.begin(),s1.end(), s2.begin(),s2.end(), sout); s1 = s3; Copyright © 2006-2017 Curt Hill

Insert Iterators An insert iterator is a form of output iterator Most iterators we have looked at are for looking at the container class An output iterator may change the class However, sets do not allow insert iterators Copyright © 2006-2017 Curt Hill

So? The set only allows const_iterators We store the output in any other STL container class In our example a vector Then put into set This shows the flexibility STL has in dealing with containers of differing organization Lets look at this one more time Copyright © 2006-2017 Curt Hill

Example vector<float> temp(set1.size() + set2.size()); vector<float>::iterator iter; iter = set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), temp.begin()); temp.resize(iter-temp.begin()); res_set = set<float>(temp.begin(), temp.end()); Copyright © 2006-2017 Curt Hill

Other set operations Intersection Difference (regular) Same form as the union but name is set_intersection Difference (regular) Same form as the union but name is set_difference The result has all those in A but not B Difference (irregular) set_symmetric_difference Produces the union of both set differences Copyright © 2006-2017 Curt Hill

Subsets There is no method for subset Construct them with union and equality A  B == (A  B == B && A != B) A  B == (A  B == B) This is not pretty using the STL operations You may be better off iterating both set simulaneously Copyright © 2006-2017 Curt Hill

STL Bags or Multisets Pretty much the same as sets with a few exceptions Still use the <set> include Class name is multiset The insert no longer returns a pair since insertion should always work Copyright © 2006-2017 Curt Hill

Bitsets in the STL An unusual STL container class It is a template class but the template is not a type but the size of the bitstring Each bitstring is fixed size Copyright © 2006-2017 Curt Hill

Constructors bitset<20> b; Creates a set that can handle zero to 19 Usually the N value should be divisible by 8 bitset<128> b(i); Converts i to a bitstring and initializes b with it Copyright © 2006-2017 Curt Hill

Operators The usual operators are allowed =, &=, |=, ^=, >>=, <<= ==, != >>, <<, |, &, ! Copyright © 2006-2017 Curt Hill

Methods any() – true if not empty none() – true if empty count() – cardinality flip() – reverse every bit reset() – sets all bits to zero reset(int) – set just this position to zero Copyright © 2006-2017 Curt Hill

More methods set() – sets all bits to one set(int) – sets just one bit to one size() – returns the template parameter to_string() – returns a string that has a 0 or 1 for each position There is a contructor that allows this to be constructed back into a set test(int) – returns true if this position is one Copyright © 2006-2017 Curt Hill

Finally The STL Set is a container class that maintains its unique items in sorted order It is easy to insert to and query The algorithms set_union, set_intersection, set_difference may be applied to any sorted container class They are very general, but not near as easy as they should be Copyright © 2006-2017 Curt Hill