What remains Topics Assignments Final exam

Slides:



Advertisements
Similar presentations
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
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.
Templates and the STL.
Object Oriented Data Structures
Writing Your Own STL Container Ray Lischner
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
C++ Review STL 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.
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.
Final Exam Review COP4530.
Object-Oriented Programming (OOP) Lecture No. 41
CS212: Object Oriented Analysis and Design
Data Structures Interview Questions.
Regarding homework 9 Many low grades
Exceptions, Templates, and the Standard Template Library (STL)
Homework 4 questions???.
Standard Template Library (STL)
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Starting Out with C++ Early Objects Eighth Edition
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Assignment 3 Biggest deductions No decomposition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
structures and their relationships." - Linus Torvalds
How can this be simplified?
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
structures and their relationships." - Linus Torvalds
Associative Structures
Chapter 18: Linked Lists.
Final Exam Review COP4530.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Recitation Outline C++ STL associative containers Examples
Graphs.
Chapter 16 Linked Structures
Standard Version of Starting Out with C++, 4th Edition
Lists - I The List ADT.
Lists - I The List ADT.
Pointers & Dynamic Data Structures
Iterators and STL Containers
Standard Template Library
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Collections Intro What is the STL? Templates, collections, & iterators
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.
structures and their relationships." - Linus Torvalds
Standard Template Library
Presentation transcript:

What remains Topics Assignments Final exam C++ standard template library (Stl) Graphs Sorting Assignments Ass. 6 (BST) – due April 16 Ass. 7 (stl set) – due april 23 Ass. 8 (graph) – due may 7 Final exam Mon. May 14, 12:50 to 2:50 in UU 206

Exam 2 Question 3 - comparing space needed for n items Vector - O(capacity) capacity may be > n Linked list - O(n) Question 6 - void print_in_reverse(node* p){ if(p == nullptr) return; print_in_reverse(p->next); cout << p-data; } Question 11 – for array in which items are stored in order of key Add(item) – O(n) Remove(item) – O(n) Retrieve(key) – o(log2 n)

Some Assignment 5 issues Use names specified in assignment Lower case m for file names Upper case m for class name Add method should not add (or replace) element if there is already an element with that key Should map.cpp do any terminal output? Test with goal of finding errors! what are the problems with implementing the copy constructor by calling the add method?

Two problems The copy and original have separate nodes, but share the memory allocated for the value part of the elements in the original what is the big O?

standard template library (STL) Most programs need to store a collection of like elements Most programming languages now have types (classes) that programmers can use Java: collection classes Python: list and map C++ added the standard template library in 1998 Stl was updated in 2011 and 2014

Before class on Thursday zybooks ch.22 – sections 1, 4 and 5

Stl components Containers iterators algorithms

Some Stl container classes Sequence containers Vector List Forward_list - added in 2011 Container adapters Stack Queue PRIORITy_queue Associative containers Map Set Unordered associative containers Unordered_map - added in 2011 Unordered_set - added in 2011

Stl containers Are templates for creating a class Ex: vector<t> vector<int> numbers; vector<string> words; Vector<itemTopurchase> cart;

Compare to use of typedef Who decides on type of element to be stored in the container? User or implementer? When is executable code (a .o file) created? Can a program make use of multiple containers holding different types? Yes or no

Compare to use of typedef Who decides on type of element to be stored in the container? template class – User Typedef – implementer (writer of .h file) When is executable code (a .o file) created? Typedef – when the .cpp file is compiled Template class – when the file in which the class is used is compiled Can a program make use of multiple containers holding different types? Template class – Yes Typedef - no

How to make our own class a template class see zybooks ch How to make our own class a template class see zybooks ch.18 (sec 1 and 2)

WHY? // file Stack.h #ifndef _STACK_ #define _STACK_ template<class ItemType> class Stack public: Stack(); bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); ItemType peek() const; private: ; #include "Stack.cpp" #endif; WHY?

// file: Stack.cpp #include "Stack.h" template<class ItemType> Stack<ItemType>::Stack() { } bool Stack<ItemType>::push(const ItemType& newEntry) { bool Stack<ItemType>::isEmpty() const { bool Stack<ItemType>::pop(){ ItemType Stack<ItemType>::peek() const{

iterators An iterator is an object that provides access to an element in a container Provide same functionality as array indices and linked list node pointers All sequence and associative classes have these two methods: C.begin() - returns an iterator to the first element in the container C.end() - Returns an iterator referring to the past-the-end element Many stl container methods return an iterator Ex: find

iterators Iterator objects have at least the following 3 methods Iter++ // iter now gives access to the “next” element *iter // the value of the element accessed by iter == and !== // do two iterators access the same element? challenge - Using the begin and end methods and the iterator methods above write a loop that traverses a vector and displays each element

vector<string> words; // add strings to words auto iter = words.begin(); for (iter = words.begin(); iter != words.end(); iter++) { cout << *iter << endl; } vector<string> words; // add strings to words auto iter = words.begin(); while (iter != words.end(){ cout << *iter << endl; iter++; }

Range-based for statement Shorthand version of for loop using iterators For ( item : container) statement Can be used with any stl container which has a begin() and an end() method

Two examples vector<string> words; // add strings to words for (auto word : words) { cout << word << endl; } vector<int> numbers; // add integers to numbers for (auto &num : numbers) { num = num * 2; } size of a container cannot be changed using a range for statement

Stl Associative containers Elements ordered by key Map associative array; hold key-value pair Set container in which the key is the value Multimap map in which a key can appear multiple times Multiset set in which a key can appear multiple times Unordered collections Unordered_map map organized by a hash function Unordered_set set organized by a hash function Unordered_multimap unordered map in which a key can appear multiple times Unordered_multiset unordered set in which a key can appear multiple times

map<K, T> methods set<T> methods map<K, T> methods Big 3 Begin() End() Empty(), size(), clear() Insert(T), emplace(T) Erase(T) Count(T) - returns 0 or 1 Find(T) - returns an iterator Big 3 Begin() End() Empty(), size(), clear() Insert(k, t), emplace(k, t) Erase(k) Count(k) - returns 0 or 1 Find(k) - returns an iterator At(k) or [k]

Using at(k) or [k] with a map K is mapped to (associated with) a value Can use a map key in the same way we use an array index My_array[i] = T; // assigns t to the I’th position in my_array My_map[k] = t; // makes t the value associated with k or adds new element My_map.at(k) = t; // makes t the value associated with k (throws exception if k not found) t = my_array[i]; // t is assigned the value at position i T = my_Map[k]; // t is assigned the value associated with k t = my_map.at(k); // t is assigned the value associated with k (throws exception if k not found)

Two questions How are the stl set<t> and map<t> classes implemented? What is the big o of the insert, emplace, erase, find, count, at and [ ] methods?

Two questions How are the stl set<t> and map<t> classes implemented? Balanced binary search tree What is the big o of the insert, emplace, erase, find and count methods? Logarithmic in size - O(log2 n)

Unordered_set<t> and Unordered_ map<k, t> Are implemented using a form of hash table Have same methods as set<t> and Map<K, t> big O (from cplusplus.com) Average case: constant - O(1) Worst case: linear in container size - O(n) Can use default hash function and table size provided or provide your own

Assignment 7 Will compare the performance of the set<T> and unordered_set<t> Be prepared to ask questions next Tuesday

graphs What is a graph? Interface for a graph adt Data structures for implementing a graph Comparing use of space and time Traversing a graph Foundation for many graph algorithms

By next Tuesday’s class zybooks chapter 23 sections 1, 2, 3, 6

A graph is a collection of items having a many to many relationship

examples computer networks airline route maps road map pert charts social networks

Graphs have vertices and edges

Some Graph variations undirected graph (graph) edges are bidirectional directed graph (digraph) edges go in one direction only edges may be weighted or unweighted

An unweighted digraph

graph Interface Add_Vertex(v) – add a vertex to the graph Add_Edge(v1, v2) - add an edge to the graph isVertex(v) – is v a vertex in the graph? Is_Edge(v1, v2) – is there an edge from v1 to v2 in the graph? Remove_Vertex(v) – remove v from the graph Remove_Edge(v1, v2) – remove an edge from the graph

Data structures for implementing a graph A Graph consists of vertices and edges Vertices have unique identifiers (often strings) City name Computer id name Edges are made up of 2 vertices Undirected edge - (v1, v2) Directed edge - <v1, v2> Look first at storing the vertices

Storing the vertices 1 2 3 4 A B C D E for the graph user vertices are identifiers (strings) Graph implementations are based on dealing with vertices as numbers Vertices are stored in a vector the number of a vertex is the index at which it is stored Vertex table 1 2 3 4 A B C D E

Using the vertex table most graph operations start by looking up the number associated with a vertex Int vnum = getvnum(vname); What is the big O? Also need the reverse operation String vname = getvname(vnum); What is the big(O)?

int vnum = getvnum(vname); O(n) vertexTable A D C B E 0 1 2 3 4 int vnum = getvnum(vname); O(n) string vname = getvname(vnum); O(1)

How many edges does a graph with v vertices have?

Storing the edges A graph has between 0 and v2 edges Adjacency matrix based on storing information about all possible edges Adjacency lists based on storing information about only the existing edges

Storing the edges of a graph

Vertex table 1 2 3 4 A B C D E 1 2 3 4 1 2 3 4 0 1 2 3 4 1 2 3 4 Adjacency list Adjacency matrix

Vertex table 1 2 3 4 A B C D E 1 2 3 4 1 2 3 4 0 1 2 3 4 1 2 4 2 1 2 3 4 1 1 3 2 Adjacency list Adjacency matrix