C++ Standard Template Library

Similar documents
C++ 11 and the Standard Library: Containers, Iterators, Algorithms

TDDD38 - Advanced programming in C++

Advanced C++ STL. Tony Wong

CIS 190: C/C++ Programming. Lecture 12 Student Choice

Summary. Design. Layout

Lesson 13 - Vectors Dynamic Data Storage

STL Quick Reference for CS 241

3. Fundamental Data Structures

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Linked Lists and Abstract Data Structures A brief comparison

DATA STRUCTURE AND ALGORITHM USING PYTHON

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Course Review. Cpt S 223 Fall 2009

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

Course Review for Finals. Cpt S 223 Fall 2008

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

LECTURE 11 TREE TRAVERSALS

CSE 100: GRAPH ALGORITHMS

STL: C++ Standard Library

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

4.1 Interval Scheduling

Heapsort. Heap data structure

Data Structures Question Bank Multiple Choice

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CH. 8 PRIORITY QUEUES AND HEAPS

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CH 8. HEAPS AND PRIORITY QUEUES

Object-Oriented Programming

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Dynamic Data Structures

Heaps. A heap is a certain kind of complete binary tree.

Lecture 18 Heaps and Priority Queues

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

9. Heap : Priority Queue

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Algorithms and Data Structures

Standard Library Reference

ECE 250 Algorithms and Data Structures

Tree traversals and binary trees

use static size for this buffer

Standard Template Library

CISC 3130 Data Structures Spring 2018

Discuss the following operations on One-Dimensional array with algorithms.

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Priority Queues 1 / 15

Data Structure. IBPS SO (IT- Officer) Exam 2017

Question Paper Code : 97044

MCA SEM-II Data Structure

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

Priority Queues and Huffman Trees

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

CSI33 Data Structures

Algorithm Analysis Advanced Data Structure. Chung-Ang University, Jaesung Lee

Week 6. Data structures

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

STL Standard Template Library

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Summer Final Exam Review Session August 5, 2009

Object-Oriented Programming for Scientific Computing

Course Review. Cpt S 223 Fall 2010

CSCI 104 Binary Trees / Priority Queues / Heaps. Mark Redekopp Michael Crowley

Sample Question Paper

CPSC 427: Object-Oriented Programming

1 P age DS & OOPS / UNIT II

Lecture 6 Sorting and Searching

Programming in C++ using STL. Rex Jaeschke

Basic Data Structures (Version 7) Name:

CSE 12 Week Eight, Lecture One

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Abstract Data Types 1

and 6.855J February 6, Data Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Lecture Notes on Priority Queues

An Introduction to Trees

The Bucharest University of Economic Studies. Data Structures. Heap Priority Queues Maps and Sets

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Run Time Environment

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

UNIT 3

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Stores a collection of elements each with an associated key value

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CS 62 Practice Final SOLUTIONS

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Data Structures and Algorithms Notes

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Stack and Queue. Stack:

Notes. Video Game AI: Lecture 5 Planning for Pathfinding. Lecture Overview. Knowledge vs Search. Jonathan Schaeffer this Friday

Transcription:

C++ Template Library Comp Sci 1575 Data Structures

Outline 1 2 3 4

Data structures Color key:

Array sorting algorithms

Outline 1 2 3 4

is part of standard namespace Old diagram of the standard

Outline 1 2 3 4

Main pillars of the manage storage space for elements and provide member functions to access them. Implemented as class templates, with flexibility of types as elements. act on containers, and perform operations like initialization, sorting, searching, and transforming of the contents of containers. step through elements of collections of objects in containers or subsets of containers. Pointer-like iterators can traverse many container classes.

Example std :: vector Check out the code: container with iterators and algorithms A dynamic C-like array (i.e., capable of random access) with the ability to resize itself automatically when inserting or erasing an object. Random access - constant O(1). Insertion or removal of elements at the back, average time O(1). Inserting an element to the back of the vector at the end takes amortized constant time. Removing the last element takes only constant time, because no re-sizing happens. Insertion or removal of elements - linear in distance to the end of the vector O(n). Inserting and erasing at the beginning or in the middle is linear in time. See: Intro vector.cpp

Outline 1 2 3 4

The is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like queues, lists and stacks. There are three classes of containers sequence containers, associative containers, and unordered associative containers each of which is designed to support a different set of operations. The container manages the storage space that is allocated for its elements and provides member functions to access them, either directly or through iterators (objects with properties similar to pointers). Most containers have at least several member functions in common, and share functionalities. Which container is the best for the particular application depends not only on the offered functionality, but also on its efficiency for different workloads.

For a comprehensive list, see: http://en.cppreference.com/w/cpp/container http://www.cplusplus.com/reference/stl/ https://en.wikipedia.org/wiki/_ Template_Library

How to choose your container? Older version

How to choose your container? Newer version

Outline 1 2 3 4

Where does slist go? http://en.cppreference.com/w/cpp/iterator http://www.cplusplus.com/reference/iterator/

Outline 1 2 3 4

: example of heap functions Defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Range is defined as [first, last) where last refers to the element past the last element to inspect or modify. http://en.cppreference.com/w/cpp/algorithm/ http://www.cplusplus.com/reference/algorithm/ For example, heap operations can be performed on a vector: is heap checks if the given range is a max heap is heap until finds the largest subrange that is a max heap make heap creates a max heap out of a range of elements push heap adds last-1 element to a max heap pop heap removes the largest element from a max heap by moving to end sort heap turns a max heap into a range of elements sorted in ascending order

: example of heap functions See: Heap algorithms.cpp How can we do this more directly?

Outline 1 2 3 4

std::priority queue #include < queue > A priority queue is a container adaptor that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction. A user-provided Compare can be supplied to change the ordering, e.g. using std::greater< T > would cause the smallest element to appear as the top(). Working with a priority queue is similar to managing a heap in some random access container, with the benefit of not being able to accidentally invalidate the heap.

std::priority queue template parameters template< c l a s s T, c l a s s C o n t a i n e r = s t d : : v e c t o r <T>, c l a s s Compare = s t d : : l e s s <typename C o n t a i n e r : : v a l u e t y p e > > T - The type of the stored elements. The behavior is undefined if T is not the same type as Container::value type. Container - Type of underlying container to store the elements. Container must satisfy requirements of SequenceContainer, and its iterators must satisfy the requirements of RandomAccessIterator. It must provide the following functions with the usual semantics: front(); push back(); pop back(); containers std::vector and std::deque satisfy these requirements. Compare - type providing a strict weak ordering.

Demo code: priority queue See: Priority queue.cpp