CSCI 3110.  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
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.
1 Associative Containers Gordon College Prof. Brinton.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
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
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
C++ STL CSCI 3110.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Associative Containers Sets Maps Section 4.8. Associative Containers.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
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.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
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
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
What remains Topics Assignments Final exam
Associative Structures
Containers & Iterators
A Sorted, Unique Key Container
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
CS212: Object Oriented Analysis and Design
Recitation Outline C++ STL associative containers Examples
Iterators and STL Containers
Chapter 3 Lists, Stacks, and Queues
Recitation Outline Hash tables in C++ STL Examples Recursive example
The List Container and Iterators
A dictionary lookup mechanism
Presentation transcript:

CSCI 3110

 Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than position (Account #, ID, SSN, …) ◦ set ◦ multiset ◦ map ◦ multimap

 Container Adapters – specialized interfaces to general containers ◦ stack ◦ queue ◦ priority_queue

 Stores elements based on a key  Key can consist of one or more attributes to uniquely identify each element (we will assume only one attribute).  Example: Department of Motor Vehicles (DMV) uses license-plate # to identify a vehicle.  Similar to vector & list – it is another storage structure with operations to access & modify elements.  Main difference is that associative-container uses the key rather than an index (vector) or linear search (list) to retrieve an element.

 Stores a set of values (i.e., “keys”)  Values are unique (stored only once)  Implemented as a balanced binary search tree (red-black tree) ◦ #include ◦ set s;  Fast insert and delete ◦ insert, erase  Fast search ◦ find  Other operations ◦ size, empty, clear,... “if” “operator” “while” “class” “template”

 Stores a set of values (i.e., “keys”)  Like set, but values need not be unique  Implemented as a balanced binary search tree (red-black tree) ◦ #include ◦ multiset ms;  Fast insert and delete ◦ insert, erase  Fast search ◦ find  Other operations ◦ size, empty, clear,...

 Stores a set of (key, value) pairs  Each key has one value  Implemented as a balanced binary search tree (red-black tree) #include //define a map with //keys of type string //and values of int map m;  Fast insert and delete m[“fred”] = 99; insert, erase “fred” | 99 “sue” | 86 “james” | 52

 Fast search ◦ int x = m[“fred”]; ◦ find  Other operations ◦ size, empty, clear,...

 STL associative containers are implemented internally using a red-black tree which is a BST ◦ Key classes stored in associative containers must implement bool operator< (T other) ◦ If they don’t, you can alternately pass a comparator class to the template that it should use to order elements ◦ A comparator class overrides bool operator() (T a, T b);

set employees; //BST sorts based on pointers //(probably not what you want) class EmployeeComparator { public: bool operator() (const Employee* a, const Employee* b) { return (a->getID() getID()); } }; //create a set that sorts based on employee IDs set employees;

 Classes that will be stored in STL containers should explicitly define the following: ◦ Default constructor ◦ Copy constructor ◦ Destructor ◦ operator = ◦ operator== ◦ operator<  Not all of these are always necessary, but it might be easier to define them than to figure out which ones you actually need  Many STL programming errors can be traced to omitting or improperly defining these methods

Copy constructor:  Copy constructor: map m; map m2(m);

 An STL map is implemented as a tree- structure, where each node holds a “pair”  Most important to know when retrieving data from the table ◦ Some functions return the pair, not just the value  A pair has two fields, first (holding the key) and second (holding the value)

 If you have a pair object, you can use the following code to print the key and value: cout << myPairObject.first << “ “ << myPairObject.second;  If you have a pointer to the pair object, use the arrow operator instead cout first second;

 Tree structure ◦ logarithmic time inserts, finds, deletes

void clear() remove all elements bool empty() returns true if empty, false otherwise size_type max_size() returns max number of elements map can hold (usually an integer returned) [capacity] size_type size() return the number of elements currently in the map (usually an integer returned) [actual size]

iterator begin() returns an iterator to the first element in the map (the first when sorted, due to storage mechanism) iterator end() returns an iterator to the last element in the map (the last when sorted) reverse_iterator rbegin() returns a reverse iterator to the end of the map reverse_iterator rend() returns a reverse iterator to the start of the map

pair insert(const value_type &val) Insert val into the map, if it’s not already there. Return pair if successful, pair if fails. iterator insert(iterator I, const value_type &val) Insert val into the map, after the value specified by i. Iterator to inserted element is returned. template void insert(InIter start, InIter end) Insert a range of elements

void erase (iterator i) Remove the element pointed to by i. size_type erase(const key_type & k) Remove from the map elements that have keys with the value k. void erase(iterator start, iterator end) Remove the elements in the range start to end

iterator find(const key_type &k) Returns an iterator to the specified key. If the key is not found, an iterator to the end of the map is returned. size_type count(const key_type &k) const Returns the number of times a key k occurs in the map (0 or 1) reference operator[](const key_type &k) Returns a reference to the value associated with the key k. If the key is not found in the map, the key and a default constructed instance of the value type is inserted in the amp.

iterator lower_bound(const key_type &k) Returns an iterator to the first element in the map with a key >= k iterator upper_bound(const key_type &k) const Returns an iterator to the first element in the map with a key strictly > k pair equal_range(const key_type &k) Returns a pair of iterators that point to the upper bound and the lower bound in the map for the specified key

Map between characters and ASCII representations Map between Characters and ASCII representations

Same program, exploiting []

Print all entries in map in forward and reverse order

Storing objects, requiring an overload of the < operator for the key type