C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Concept= a set of abstractions (e.g., types) (Generic Programming) Programming with Concepts defined by a set of requirements {vector, deque, list, set,
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
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.
1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.
The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai.
Chapter 19 Java Data Structures
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
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)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Intro to the C++ STL Timmie Smith September 6, 2001.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
The Standard Template Library Container Classes Version 1.0.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
Chapter 8 Writing Generic Functions. Objectives Understand the use of generic functions. Learn about the use of templates, their advantages and pitfalls.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
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.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
CS212: Object Oriented Analysis and Design
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
18 – Sequential Containers
Chapter 22: Standard Template Library (STL)
Associative Structures
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
CS212: Object Oriented Analysis and Design
Elements are always copied when they are put into a container
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Standard Template Library
Presentation transcript:

C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually, the sort implementation is a binary tree which implies that elements cannot be changed directly. Thus, an element has to be deleted, modified, and then re-inserted. – Made up of a key or search criteria. – Use the insert() method in assigning elements, and erase() method for deletion. Also, emplace() will copy an element with the arguments passed in and return the position. – Associative container based upon sorting of the element values. – Ability to have a default sorting operation when declared. – Use the #include directive to pick up set support.

C++ Maps and Multimaps Where sets sort elements based upon the association of the value of the element itself, maps and multimaps sort elements based upon some key in the element associated with the value of the element. Multimaps allow duplicate key/value pairs where as maps do not. – The key and value of a map or multimap must be copyable/movable. – The key must be comparable based upon the sorting criteria. – Usually binary tree based sorting. – Like sets, the key cannot be changed directly due to the side effect of compromising the order. – The element must be removed, modified, and then re-inserted as is the case with a set.

C++ SetsMap Template i/f f(n) Hidden

C++ Unordered Sets and Maps Unordered containers are primarily considered hash tables where elements that are represented as part of the container are placed into the container in some arbitrary order. This is to say the container is a bucket that that elements are dropped into and then retrieved in some random fashion. With regular maps and sets, there is no sorting criteria that is followed by the user in order to place an element into a specific position – it is hidden from the user of the class. – Must use the #include directive with and/or with possibly the include needed. – Hash tables use a hash code associated with a linked list of some sort. – There must be at least a forward iterator. – Rehashing is possible either through a complete rearrangement of data or some incremental rearrangement.

C++ Unordered Container (Hash Table) Hash f(n) Hidden Template i/f

C++ – No direct access operations provided and indirect access with iterators makes key values constant. – As a developer, the things that can be defined are: the number of buckets, user defined hash function, equivalence function, and load factor. Also, outside of the load factor, a user can force a rehash. See “When to Use Which Container”, Section 7.12, pgs. 392 – 395, The C++ Standard Library (Josuttis).

C++ Function Objects and Lambdas – Function Object: functor (f(n)) has a call to operator() f(n) object may have state f(n) object has its own type f(n) object is usually quicker than a function pointer. – Lambdas Convenient method of specifying algorithm and function details Very simple way of defining some behavior that may only be done is some other cryptic fashion

C++ Iterators Iterators are objects that traverse sequences of elements and are defined with a formal API based upon element references (pointers). Output Iterators Input Iterators Forward Iterators Bidirectional Iterators Random Access Iterators – Output Iterators Output iterators only steps forward with a write. An output iterator can only iterate over a range of elements once. Common output iterators are ostreams and inserters (e.g. cout). – Input Iterators Input iterators only steps forward with a read and only reads the element once. Common input iterators are istreams such as ‘cin’ – Forward Iterators Input iterators that can be compared to another input iterator pointing to the same element. Forward lists and unordered containers provide these types of iterators.

C++ – Bidirectional Iterators Forward iterators that can also traverse elements in the reverse or backward direction. List, maps, and set containers provide bidirectional iterators. – Random Access Iterators Ability to perform iterator arithmetic as well as all of the functions that bidirectional iterators perform. All of the random access containers can perform these type of iterators (i.e. array, vector, deques and strings).

C++

Algorithms C++ Algorithms process iterator ranges and allow operations passed into the algorithm that will be used internally. The ranges for iterators must be valid which is usually specified as.begin() and.end() of the same iterator. 1.Modifying, Removing, and Non-modifying algorithms 2.Sorting, Mutating, and Sorted-range algorithms 3.Numeric algorithms – Modifying, Removing, and Non-modifying algorithms Modifying algorithms all change in values of elements whereas the Non-modifying algorithms do not. In general Non-modifying algorithms return some predicate return value or something about the element(s) (e.g. find_if() or count() ) – Sorting, Mutating, and Sorted-range algorithms Mutating algorithms change the order of elements by changing the values of the elements (e.g. assigning new values or swapping values). Sorting algorithms are special cases of mutating algorithms in that the order of elements are changed but not due to changing values of elements rather the indexes of where the elements can be found change.

C++ – Numeric algorithms Algorithms that combine the values of numeric elements in various ways. For instance, accumulate() will process the sum of all of the elements. Specific Algorithms – for_each() template Function for_each (InputIterator first, InputIterator last, Function fn); first, last Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. fn Unary function that accepts an element in the range as argument. This can either be a function pointer or a move constructible function object. Its return value, if any, is ignored. – count() template typename iterator_traits ::difference_type count (InputIterator first, InputIterator last, const T& val);

C++ first, last Input iterators to the initial and final positions of the sequence of elements. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. val Value to match. T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side). – find() template InputIterator find (InputIterator first, InputIterator last, const T& val); first, last Input iterators to the initial and final positions in a sequence. The range searched is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. val Value to search for in the range. T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).

C++ – search() template ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); first1, last1 Forward iterators to the initial and final positions of the searched sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1. first2, last2 Forward iterators to the initial and final positions of the sequence to be searched for. The range used is (first2,last2). For (1), the elements in both ranges shall be of types comparable using operator== (with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands). pred Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible to bool. The returned value indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

C++ – remove() template ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); first, last Forward iterators to the initial and final positions in a sequence of move-assignable elements supporting being compared to a value of type T. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. val Value to be removed. – sort() template void sort (RandomAccessIterator first, RandomAccessIterator last); template void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); first, last Random-access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable.