CS 103 Unit 12 Slides

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

STL. Prof Tejada Week 14

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

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

CSCI 104 Templates. Mark Redekopp David Kempe

CSCI 104 Overview. Mark Redekopp David Kempe

CSCI 104 Templates. Mark Redekopp David Kempe

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

CS 103 Unit 11. Linked Lists. Mark Redekopp

Standard Template Library

CS 103 Unit 13 Slides

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

1. The term STL stands for?

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

SSE2034: System Software Experiment 3

CS 103 Unit 11. Linked Lists. Mark Redekopp

Lectures 11,12. Online documentation & links

Queue Implementations

Chapter 17: Linked Lists

CSCI 104 Classes. Mark Redekopp David Kempe

STL: C++ Standard Library

Templates and Vectors

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

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

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CS197c: Programming in C++

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

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

Lesson 13 - Vectors Dynamic Data Storage

EE 355 Unit 13 C++ STL; Iterators, Maps, Sets. Mark Redekopp

Chapter 17: Linked Lists

COEN244: Class & function templates

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

CSI33 Data Structures

Dynamic Data Structures

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

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

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

CSCI 104 C++ STL; Iterators, Maps, Sets. Mark Redekopp David Kempe

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

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

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

1 Short Answer (7 Points Each)

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

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

Patterns: Working with Arrays

Exercise 6.2 A generic container class

CSCI 104 C++ STL; Iterators, Maps, Sets. Mark Redekopp David Kempe

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

2014 Spring CS140 Final Exam - James Plank

19.1 The Standard Template Library

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

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

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

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

Computational Physics

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Associative Containers and Iterators. Ali Malik

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

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

CSCI 104 Polymorphism. Mark Redekopp David Kempe

by Pearson Education, Inc. All Rights Reserved. 2

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Short Notes of CS201

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS201 - Introduction to Programming Glossary By

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

C++ Lab 04 - File I/O, Arrays and Vectors

Intermediate Programming, Spring 2017*

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

C++ For Science and Engineering Lecture 15

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

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

Programming Abstractions

Lecture on pointers, references, and arrays and vectors

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

ECE 2400 Computer Systems Programming Fall 2017 Topic 15: C++ Templates

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Computer Science II Lecture 2 Strings, Vectors and Recursion

W3101: Programming Languages C++ Ramana Isukapalli

Concurrent Data Structures in C++ CSInParallel Project

Linked List using a Sentinel

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

pointers & references

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

Sequence Containers. Cristian Cibils

G52CPP C++ Programming Lecture 18

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

STL Quick Reference for CS 241

CS 11 C++ track: lecture 1

A brief introduction to C++

1 Unit 8 'for' Loops

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Transcription:

1 CS 103 Unit 12 Slides Standard Template Library Vectors & Deques Mark Redekopp

2 Templates We ve built a list to store integers But what if we want a list of double s or char s or other objects We would have to define the same code but with different types What a waste! Enter C++ Templates Allows the one set of code to work for any type the programmer wants struct IntItem int val; IntItem *next; ; class ListInt public: ListInt(); // Constructor ~ListInt(); // Destructor void push_back(int newval);... private: IntItem *head; ; struct DoubleItem double val; DoubleItem *next; ; class ListDouble public: ListDouble(); // Constructor ~ListDouble(); // Destructor void push_back(double newval);... private: DoubleItem *head; ;

3 Templates Enter C++ Templates Allows the type of variable to be a parameter specified by the programmer Compiler will generate separate class/struct code versions for any type desired (i.e instantiated as an object) List<int> my_int_list causes an int version of the code to be generated by the compiler List<double> my_dbl_list causes a double version of the code to be generated by the compiler // declaring templatized code template <typename T> struct Item T val; Item<T> *next; ; template <typename T> class List public: List(); // Constructor ~List(); // Destructor void push_back(t newval);... private: Item<T> *head; ; // Using templatized code // (instantiating templatized objects) int main() List<int> my_int_list(); List<double> my_dbl_list(); my_int_list.push_back(5); my_dbl_list.push_back(5.5125); double x = my_dbl_list.pop_front(); int y = my_int_list.pop_front(); return 0;

4 C++ STL C++ has defined a whole set of templatized classes for you to use out of the box Known as the Standard Template Library (STL)

5 Vector Class 1 2 3 4 Container class (what it contains is up to you via a template) Mimics an array where we have an indexed set of homogenous objects Resizes automatically my_vec my_vec my_vec my_vec 50 51 52 53 54 30 5 6 51 52 53 54 10 8 5 30 51 52 53 54 30 43 51 53 54 10 5 10 1 2 3 4 #include <iostream> #include <vector> using namespace std; int main() vector<int> my_vec(5); // init. size of 5 for(unsigned int i=0; i < 5; i++) my_vec[i] = i+50; my_vec.push_back(10); my_vec.push_back(8); my_vec[0] = 30; unsigned int i; for(i=0; i < my_vec.size(); i++) cout << my_vec[i] << " "; cout << endl; int x = my_vec.back(); // gets back val. x += my_vec.front(); // gets front val. // x is now 38; cout << "x is " << x << endl; my_vec.pop_back(); my_vec.erase(my_vec.begin() + 2); my_vec.insert(my_vec.begin() + 1, 43); return 0;

6 constructor Can pass an initial number of items or leave blank operator[ ] Allows array style indexed access (e.g. myvec[i]) push_back(t new_val) Adds a copy of new_val to the end of the array allocating more memory if necessary size(), empty() Size returns the current number of items stored as an unsigned int Empty returns True if no items in the vector Vector Class pop_back() Removes the item at the back of the vector (does not return it) front(), back() Return item at front or back erase(index) Removes item at specified index (use begin() + index) insert(index, T new_val) Adds new_val at specified index (use begin() + index) #include <iostream> #include <vector> using namespace std; int main() vector<int> my_vec(5); // 5= init. size for(unsigned int i=0; i < 5; i++) my_vec[i] = i+50; my_vec.push_back(10); my_vec.push_back(8); my_vec[0] = 30; for(int i=0; i < my_vec.size(); i++) cout << my_vec[i] << " "; cout << endl; int x = my_vec.back(); // gets back val. x += my_vec.front(); // gets front val. // x is now 38; cout << "x is " << x << endl; my_vec.pop_back(); my_vec.erase(my_vec.begin() + 2); my_vec.insert(my_vec.begin() + 1, 43); return 0;

7 Vector Suggestions If you don t provide an initial size to the vector, you must add items using push_back() When iterating over the items with a for loop, use an 'unsigned int' When adding an item, a copy will be made to add to the vector #include <iostream> #include <vector> using namespace std; int main() vector<int> my_vec; for(int i=0; i < 5; i++) // my_vec[i] = i+50; // doesn t work my_vec.push_back(i+50); for(unsigned int i=0; i < my_vec.size(); i++) cout << my_vec[i] << " " cout << endl; do_something(myvec); // copy of myvec passed return 0; void do_something(vector<int> v) // process v;

8 Your Turn In-class Exercises vector_eg middle concat parity_counts rpn

9 Understanding Performance Vectors are good at some things and worse at others in terms of performance The Good: Fast access for random access (i.e. indexed access such as myvec[6]) Allows for fast addition or removal of items at the back of the vector The Bad: Erasing / removing item at the front or in the middle (it will have to copy all items behind the removed item to the previous slot) Adding too many items (vector allocates more memory that needed to be used for additional push_back() s but when you exceed that size it will be forced to allocate a whole new block of memory and copy over every item After deleting we have to move everyone up 5 30 51 52 53 54 30 51 52 53 54 10 10 30 51 52 53 54 10 12 18 30 51 52 53 54 10 12 18 Vector may have 1 extra slot, but when we add 2 items a whole new block of memory must be allocated and items copied over

10 Deque Class Double-ended queues (like their name sounds) allow for additions and removals from either end of the list/queue Performance: Slightly slower at random access (i.e. array style indexing access such as: data[3]) than vector Fast at adding or removing items at front or back

11 Deque Class Similar to vector but allows for push_front() and pop_front() options Useful when we want to put things in one end of the list and take them out of the other 1 #include <iostream> #include <deque> using namespace std; int main() deque<int> my_deq; for(int i=0; i < 5; i++) my_deq.push_back(i+50); cout << At index 2 is: << my_deq[2] ; cout << endl; 1 2 3 my_deq my_deq my_deq 50 51 52 53 54 51 52 53 54 60 60 61 62 63 64 after 1 st iteration after all iterations 2 3 4 for(int i=0; i < 5; i++) int x = my_deq.front(); my_deq.push_back(x+10); my_deq.pop_front(); while(! my_deq.empty()) cout << my_deq.front() << ; my_deq.pop_front(); cout << endl; 4 my_deq