Regarding homework 9 Many low grades

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
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.
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Templates and the STL.
Object Oriented Data Structures
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
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.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Intro to the C++ STL Timmie Smith September 6, 2001.
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.
1 The Standard Template Library Drozdek Section 3.7.
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
Data Structures Interview Questions.
Programming with ANSI C ++
The Design and Analysis of Algorithms
Lecture 7-2 : STL Iterators
Exceptions, Templates, and the Standard Template Library (STL)
Homework 4 questions???.
CS 367 – Introduction to Data Structures
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
week 1 - Introduction Goals
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
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.
Collections Intro What is the STL? Templates, collections, & iterators
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
structures and their relationships." - Linus Torvalds
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
ADT Implementations: Templates and Standard Containers
Miscellaneous Stuff Which there wasn’t enough time to cover
structures and their relationships." - Linus Torvalds
Discussion section #2 HW1 questions?
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Standard Template Library Model
Graphs.
Standard Version of Starting Out with C++, 4th Edition
Iterators and STL Containers
Exceptions, Templates, and the Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
Graphs.
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
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:

Regarding homework 9 Many low grades To recover half of the points lost on Map methods Download test program used (from home page) Fix any problem(s) Bring to lab on Friday may 5 Original code with attached grading sheet Hard copy of revised map.cpp with changed or added code highlighted Run your program with provided test case for one of the instructors Be ready to answer questions about the changes you made

Using a vector to store the vertices What is the efficiency of getvnum(vname) O(n) What is the efficiency of 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 list 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

Storing the edges of a digraph

0 A 1 B 2 C 3 D 4 E

0 A 1 B 2 C 3 D 4 E How do we find out if v1 is adjacent to v2? How do we find out if v1 is adjacent from v2?

Comparing adjacency matrix and adjacency lists Given that a graph has v vertices and e edges How much memory space is needed? What is the efficiency of Add an edge from v1 to v2? Is there an edge from v1 to v2? How many vertices are adjacent to v1? How many vertices are adjacent from v1?

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

Stl components Containers iterators algorithms

containers Are templates for classes that hold a collection of like items a class to hold a specific type can be created from a template User of the class decides type of items to be stored Alternative to using typedef to define the type of items Ex: vector<t> is a template <T> is a type parameter that needs to replaced with a specific type vector<int> numbers; vector<string> words;

Any questions about homework 10?

Some Stl containers 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

Sequence container classes share many methods

Using stl container classes to implement a graph Vertex table Vector<string>; Adjacency matrix vector<vector<bool>> edges; Adjacency lists ???

Using stl container classes to implement a graph Vertex table Vector<string>; Adjacency matrix vector<vector<bool>> edges; Adjacency lists Vector<vector<int>> edges; Vector<list<int>> edges; Vector<forward_list<int> edges; Which is best? and why?

iterators Stl container classes have iterator classes associated with them Ex: <vector> defines a type: vector<T>::iterator <list> defines a type: list<t>::iterator An iterator provides access to an element in a container Provide same functionality as vector indices and linked list node pointers Iterator objects associated with all container classes have 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? Iterator objects associated with some container classes have additional methods Iterators associated with which sequence container class can be decremented?

How are iterators used? Moving through a container one element at a time all container classes (except adapters like stack and queue) have two methods Begin() // returns an iterator that accesses the container’s first element End() // returns an iterator that is positioned after the container’s last element Dereferencing it is an error Vector<string> words; // add strings to words vector<string>::iterator iter = words.begin(); // or auto iter = words.begin(); for (iter; iter != words.end(); iter++) { cout << *iter << endl; } Suppose words was defined as: forward_list<string> Iterators are the connection between containers and algorithms

<algorithm> Contains functions that operate on a range of elements in a container The range is defined by iterators Ex: words.Begin() and words.end() specifies all elements in words Some functions defined in <algorithm> Find Find_if min_element For_each Sort

Using sort Can only be used with containers that have random access iterators Two parameter version Iterators specifying range of elements to sort Uses < operator to compare elements Three parameter version Third parameter is a function that compares 2 elements