Lectures 11,12. Online documentation & links

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

CS197c: Programming in C++

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

Templates and Vectors

SSE2034: System Software Experiment 3

CS 103 Unit 12 Slides

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

The Standard Template Library Classes

Standard Template Library

2014 Spring CS140 Final Exam - James Plank

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

STL: C++ Standard Library

1. The term STL stands for?

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

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

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Programming with Haiku

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

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

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

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

CPSC 427a: Object-Oriented Programming

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

Queue Implementations

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

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

Standard Library Reference

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

Chapter 5. The Standard Template Library.

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

MODULE 33 --THE STL-- ALGORITHM PART I

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

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

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

STL

Programming in C++ using STL. Rex Jaeschke

STL. Prof Tejada Week 14

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

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

Associative Containers and Iterators. Ali Malik

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

W3101: Programming Languages C++ Ramana Isukapalli

CSI33 Data Structures

Computer Science II Lecture 2 Strings, Vectors and Recursion

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CSCE 110 PROGRAMMING FUNDAMENTALS

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Module 9. Templates & STL

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

Chapter 17: Linked Lists

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

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

STL Standard Template Library

Intermediate Programming, Spring 2017*

STL Containers, Part I

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

Standard Template Library. Outline

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Computational Physics

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

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

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Bruce Merry. IOI Training Dec 2013

Chapter 10 - Notes Applications of Arrays

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

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

Containers in C++ and Java

Standard Template Library

Due Date: See Blackboard

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Doubly-Linked Lists

Abstract Data Types 1

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

CS302. Today s Topics: More on constructor/destructor functions More on STL <list>

Stacks and their Applications

COEN244: Class & function templates

G52CPP C++ Programming Lecture 18

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

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

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

The Standard Template Library. An introduction

TDDD38 - Advanced programming in C++

CSC 222: Computer Programming II. Spring 2004

CS 103 Unit 11. Linked Lists. Mark Redekopp

Lecture on pointers, references, and arrays and vectors

PIC 10A. Lecture 23: Intro to STL containers

Chapter 17: Linked Lists

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

Computer Science II Lecture 1 Introduction and Background

SFU CMPT Topic: Function Objects

Abstract Data Types 1

Part I: Short Answer (12 questions, 65 points total)

Concepts for the C++0x Standard Library: Containers

Vectors. CIS 15 : Spring 2007

Transcription:

Lectures 11,12 1. Quicksort algorithm 2. Mergesort algorithm 3. Big O notation 4. Estimating computational efficiency of binary search, quicksort and mergesort algorithms 5. Basic Data Structures: Arrays Lists Trees Hash Tables 6. Standard Template Library of C++ Basic components of STL: containers, algorithms and iterators Online documentation & links http://www.sgi.com/tech/stl/ Several examples Taken from: http://msdn.microsoft.com/library/default.asp?url=/library/enus/vclang98/html/sample.asp An example of a container class: List Required Header: <list> reference back(); reference front(); void pop_back(); void pop_front(); void push_back(const T& x); void push_front(const T& x); The member function back returns a reference to the last element of the controlled sequence. The member function front returns a reference to the first element of the controlled sequence. 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. All the above functions require that the controlled sequence be nonempty. The member function push_back inserts an element with value x at the end of the controlled sequence. The member function push_front inserts an element with value x at the beginning of the controlled sequence.

void assign(const_iterator first, const_iterator last); void assign(size_type n, const T& x = T()); iterator erase(iterator it); iterator erase(iterator first, iterator last); bool empty() const; The first member function replaces the sequence controlled by *this with the sequence [first, last). The second member function replaces the sequence controlled by *this with a repetition of n elements of value x. The third member function removes the element of the controlled sequence pointed to by it. The fourth member function removes the elements of the controlled sequence in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists. The last member function returns true for an empty controlled sequence. #include <list> #include <iostream> using namespace std ; typedef list<int> LISTINT; int main() { //Example 1 LISTINT test; test.push_back(1); test.push_front(2); test.push_front(3); // front cout << test.front() << endl; // back cout << test.back() << endl; test.pop_front(); test.pop_back(); // middle cout << test.front() << endl; //Example 2 LISTINT listone; LISTINT listtwo; LISTINT::iterator i;

// Add some data listone.push_front (2); listone.push_front (1); listone.push_back (3); listtwo.push_front(4); listtwo.assign(listone.begin(), listone.end()); // 1 2 3 for (i = listtwo.begin(); i!= listtwo.end(); ++i) cout << *i << " "; cout << endl; listtwo.assign(4, 1); // 1 1 1 1 for (i = listtwo.begin(); i!= listtwo.end(); ++i) cout << *i << " "; cout << endl; listtwo.erase(listtwo.begin()); // 1 1 1 for (i = listtwo.begin(); i!= listtwo.end(); ++i) cout << *i << " "; cout << endl; listtwo.erase(listtwo.begin(), listtwo.end()); if (listtwo.empty()) cout << "All gone\n"; return 0; An example of an algorithm: Merge Required Header: <algorithm> 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. This version assumes that the ranges

[first1..last1) and [first2..last2) are sorted using operator<. If both ranges contain equal values, the value from the first range will be stored first. The result of merging overlapping ranges is undefined. #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; 7. First encounter with Java Documentation and downloads http://java.sun.com/ http://java.sun.com/docs/books/tutorial/getstarted/cupojava/unix.html /** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp { public static void main(string[] args) { System.out.println("Hello World!"); // Display "Hello World! docs/books/tutorial/essential/attributes/cmdlineargs.html public class Echo { public static void main (String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); http://java.sun.com/applets/