Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

Similar documents
Lectures 11,12. Online documentation & links

Template based set of collection classes STL collection types (container types)

SSE2034: System Software Experiment 3

CS 103 Unit 12 Slides

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Templates and Vectors

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

CS197c: Programming in C++

Queue Implementations

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Standard Template Library

Computer Programming

STL: C++ Standard Library

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

To know the relationships among containers, iterators, and algorithms ( 22.2).

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

Lab 7 (50 pts) Due Sunday, May 20)

1. The term STL stands for?

2014 Spring CS140 Final Exam - James Plank

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

Programming in C++ using STL. Rex Jaeschke

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

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

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Chapter 10 - Notes Applications of Arrays

Problem Set 1. You might want to read Chapter 12 of the course reader before attempting this problem.

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Computational Physics

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

To use various types of iterators with the STL algorithms ( ). To use Boolean functions to specify criteria for STL algorithms ( 23.8).

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

STL. Prof Tejada Week 14

Due Date: See Blackboard

The Standard Template Library. An introduction

Chapter 17: Linked Lists

CPSC 427a: Object-Oriented Programming

Quadratic Sorting Algorithms

Topics. bool and string types input/output library functions comments memory allocation templates classes

CS 103 Unit 11. Linked Lists. Mark Redekopp

Due Date: See Blackboard

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

Sequential Containers. Ali Malik

Lab Instructor : Jean Lai

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

CSI33 Data Structures

Lecture on pointers, references, and arrays and vectors

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

The Standard Template Library Classes

Associative Containers and Iterators. Ali Malik

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Project 1: Convex hulls and line segment intersection

Bruce Merry. IOI Training Dec 2013

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

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

MODULE 33 --THE STL-- ALGORITHM PART I

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Chapter 17: Linked Lists

CSC 222: Computer Programming II. Spring 2004

Review of the Lectures 21-26, 30-32

Intermediate Programming, Spring 2017*

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

TDDD38 - Advanced programming in C++

Due Date: See Blackboard

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Outline. Function calls and results Returning objects by value. return value optimization (RVO) Call by reference or by value?

CS101 End-Semester Examination

STL Standard Template Library

Due Date: See Blackboard

Chapter 5. The Standard Template Library.

Vectors. CIS 15 : Spring 2007

CSCE 110 PROGRAMMING FUNDAMENTALS

G52CPP C++ Programming Lecture 18

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Sequential Containers. Ali Malik

lecture09: Linked Lists

Apllications. March 03, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications.

Midterm Practice Exam

CS2255 HOMEWORK #1 Fall 2012

Project 1: Convex hulls and line segment intersection

Sequence Containers. Cristian Cibils

W3101: Programming Languages C++ Ramana Isukapalli

CS193D Handout 12 Winter 2005/2006 January 30, 2006 Introduction to Templates and The STL

Chapter 15 - C++ As A "Better C"

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Module 9. Templates & STL

Programming with Haiku

Function Templates. Consider the following function:

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Due Date: See Blackboard

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Transcription:

Lectures 19, 20, 21 1. STL library examples of applications Explanations: The member function pop_back removes the last element of the controlled sequence. The member function pop_front removes the first element of the controlled sequence. Both the above functions require that the controlled sequence is non-empty. The member function push_back inserts an element at the end of the controlled sequence. The member function push_front inserts an element at the beginning of the controlled sequence. OutputIterator merge( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result ) The merge algorithm merges two sorted sequences: [first1..last1) and [first2..last2) into a single sorted sequence starting at result. It assumes that the ranges [first1..last1) and [first2..last2) are sorted using operator<. void sort(iterator first, Iterator last); Sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. #include <iostream> #include <algorithm> #include <vector> #include <list> #include <deque> using namespace std ; int main() { const int MAX_ELEMENTS = 8 ; // Define a template class vector of int typedef vector<int> IntVector ; //Define an iterator for template class vector of ints typedef IntVector::iterator IntVectorIt ; IntVector NumbersVector(MAX_ELEMENTS) ; IntVectorIt startv, endv, itv ; // Define a template class list of int typedef list<int> IntList ;

//Define an iterator for template class list of ints typedef IntList::iterator IntListIt ; IntList NumbersList ; IntListIt first, last, itl ; // Define a template class deque of int typedef deque<int> IntDeque ; //Define an iterator for template class deque of ints typedef IntDeque::iterator IntDequeIt ; IntDeque NumbersDeque(2 * MAX_ELEMENTS) ; IntDequeIt itd ; // Initialize vector NumbersVector NumbersVector[0] = 4 ; NumbersVector[1] = 10; NumbersVector[2] = 70 ; NumbersVector[3] = 10 ; NumbersVector[4] = 30 ; NumbersVector[5] = 69 ; NumbersVector[6] = 96 ; NumbersVector[7] = 100; startv = NumbersVector.begin() ; // location of first // element of NumbersVector endv = NumbersVector.end() ; // one past the location // last element of NumbersVector // sort NumbersVector, merge requires the sequences // to be sorted sort(startv, endv) ; // print content of NumbersVector cout << "NumbersVector { " ; for(itv = startv; itv!= endv; itv++) cout << *itv << " " ; cout << " }\n" << endl ;

// Initialize vector NumbersList for(int i = 0; i < MAX_ELEMENTS; i++) NumbersList.push_back(i) ; first = NumbersList.begin() ; // location of first // element of NumbersList last = NumbersList.end() ; // one past the location // last element of NumbersList // print content of NumbersList cout << "NumbersList { " ; for(itl = first; itl!= last; itl++) cout << *itl << " " ; cout << " }\n" << endl ; // merge the elements of NumbersVector // and NumbersList and place the // results in NumbersDeque merge(startv, endv, first, last, NumbersDeque.begin()) ; cout << "After calling merge\n" << endl ; // print content of NumbersDeque cout << "NumbersDeque { " ; for(itd = NumbersDeque.begin();itd!= NumbersDeque.end(); itd++) cout << *itd << " " ; cout << " }\n" << endl ; } return 0; Output: NumbersVector { 4 10 10 30 69 70 96 100 } NumbersList { 0 1 2 3 4 5 6 7 } After calling merge NumbersDeque { 0 1 2 3 4 4 5 6 7 10 10 30 69 70 96 100 } 2. Timing - review #include <ctime> clock_t start=clock(); CODE TO BE TIMED clock_t stop=clock(); cout << (stop-start) /CLOCKS_PER_SEC << endl;

3. Generating random numbers #include <cstdlib> rand() 4. An example of timing procedure (timing STL sort algorithm on random arrays) #include <ctime> #include <cstdlib> #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { typedef vector<int> IntVector; typedef IntVector::iterator IntVectorIt; size_t n; //Seed the random-number generator with current time so that //the numbers will be different every time we run. srand((unsigned)time(null)); cout << "Enter the size of the random array to be sorted" << endl; cin >> n; cout << endl; IntVector NumbersVector(n); IntVectorIt startv,endv,itv; startv = NumbersVector.begin(); endv = NumbersVector.end(); //Enter random numbers into NumbersVector for(itv=startv;itv!=endv;itv++) *itv=rand()%n; //Start timing sorting procedure clock_t start=clock(); sort(startv,endv); //Stop timing sorting procedure clock_t stop=clock(); //Print out elapsed time in miliseconds cout << "The elapsed time: "; cout << 1000*(stop-start) /CLOCKS_PER_SEC << endl;

} return 0; 5. Merge Sort algorithm void mergesort(int a[], int s, int r) { if(r<=s) return; int m=(r+s)/2; mergesort(a, s, m); mergesort(a, m+1, r); merge(a, s, m, r); } Merge operation is discussed on page 613 of the textbook (the indexing is different than ours). Example: s=0, r=11, a=[8,7,6,5,2,3,4,11,9,10,12,1] The tree of recursive calls: The original data: 8 7 6 5 2 3 4 11 9 10 12 1 After merging on level 3: 7 8 6 2 5 3 4 11 9 10 12 1 After merging on level 2: 6 7 8 2 3 5 4 9 11 1 10 12 After merging on level 1: 2 3 5 6 7 8 1 4 9 10 11 12 After merging on level 0: 1 2 3 4 5 6 7 8 9 10 11 12

6. Analysis of Merge Sort Every execution of Merge Sort algorithm requires the number of operations of order nlog 2 n (n is the size of the array).