Sequential Containers. Ali Malik

Similar documents
Sequential Containers. Ali Malik

Sequence Containers. Cristian Cibils

Associative Containers and Iterators. Ali Malik

Ali Malik Handout 12 CS106L Winter 2018 Jan 30th, C++ Reference

STL: C++ Standard Library

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

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

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

SSE2034: System Software Experiment 3

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

Chapter 5. The Standard Template Library.

Chapter 17: Linked Lists

Standard Template Library

COMP6771 Advanced C++ Programming

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

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

TDDD38 - Advanced programming in C++

Chapter 18: Stacks And Queues

use static size for this buffer

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

Chapter 17: Linked Lists

CS24 Week 3 Lecture 1

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Computer Science II Lecture 2 Strings, Vectors and Recursion

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

Templates and Vectors

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CS 103 Unit 11. Linked Lists. Mark Redekopp

Stacks and their Applications

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

CS 103 Unit 12 Slides

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

Exercise 6.2 A generic container class

STL Quick Reference for CS 241

Containers in C++ and Java

CS24 Week 4 Lecture 2

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

Memory and Pointers written by Cathy Saxton

Introduction to Linked List: Review. Source:

Patterns: Working with Arrays

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

GridKa School 2013: Effective Analysis C++ Standard Template Library

Concurrent Data Structures in C++ CSInParallel Project

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

The Standard Template Library. EECS 211 Winter 2018

Modern and Lucid C++ for Professional Programmers. Part 7 Standard Containers & Iterators. Department I - C Plus Plus

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

STL. Prof Tejada Week 14

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

PIC 10A. Lecture 23: Intro to STL containers

Lesson 13 - Vectors Dynamic Data Storage

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Chapter 18: Stacks And Queues

CS 103 Unit 11. Linked Lists. Mark Redekopp

Priority Queues and Huffman Trees

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

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

Sixth lecture; classes, objects, reference operator.

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

auto map<deque<string>, vector<string>> mymap; for(auto iter = mymap.begin(); iter!= mymap.end(); ++iter) {

Copyright 2003 Pearson Education, Inc. Slide 1

Streams. Ali Malik

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

In Java we have the keyword null, which is the value of an uninitialized reference type

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

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

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Advanced Associative Containers. Ali Malik

1. The term STL stands for?

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

Queues and Unit Testing

Working with Batches of Data

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

STL Containers, Part I

19. Dynamic Data Structures II

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

Advanced C++ STL. Tony Wong

Prefix Trees Tables in Practice

Functions. Ali Malik

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

Queue Implementations

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

September 19,

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

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

Programming with Haiku

COEN244: Class & function templates

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

Functions. Ali Malik

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

CS 32. Lecture 5: Templates

CSE 333 Midterm Exam 5/10/13

Dynamic Data Structures

COMP6771 Advanced C++ Programming

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

COMP322 - Introduction to C++

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

COP Programming Assignment #7

Transcription:

Sequential Containers Ali Malik malikali@stanford.edu

Game Plan Recap Overview of STL Sequence Containers std::vector std::deque Container Adapters

Announcements

Recap

getline vs >> Bug favnum = fullname = What happens if we mix cin and getline? position 7 2 9 \n

getline vs >> Bug favnum = fullname = What happens if we mix cin and getline? 7 position int favnum; cin >> favnum; 2 9 \n

getline vs >> Bug favnum = fullname = What happens if we mix cin and getline? 7 position int favnum; cin >> favnum; 2 9 \n

getline vs >> Bug favnum = 729 fullname = What happens if we mix cin and getline? 7 2 9 \n position

getline vs >> Bug favnum = 729 fullname = What happens if we mix cin and getline? 7 2 9 \n position string fullname; getline(cin, fullname);

getline vs >> Bug favnum = 729 fullname = What happens if we mix cin and getline? 7 2 9 \n position string fullname; getline(cin, fullname);

getline vs >> Bug favnum = 729 fullname = "" What happens if we mix cin and getline? 7 2 9 \n position

getline vs >> Bug We can fix this by consuming the newline character after using cin! int favnum; cin >> favnum; cin >> ws; // extracts as many whitespace chars as possible from current position in stream string fullname; getline(cin, fullname); // no residual newline char in stream

Useful Aside

Structs You can define your own mini-types that bundle multiple variables together: struct point { int x; int y; }; Useful for Assignment

Structs struct point { int x; int y; }; point p; p.x = 2; p.y = ;

Overview of STL

Overview of STL As mathematicians learned to lift theorems into their most general setting, so I wanted to lift algorithms and data structures - Alex Stepanov, inventor of the STL

Overview of STL Algorithms Iterators Allocators Containers Functors/Lambdas Adapters

Overview of STL Algorithms Iterators Allocators Containers Functors/Lambdas Adapters

Where we are going... Here is a program that generates a vector with random entries, sorts it, and prints it, all in one go! const int knumints = 200; std::vector<int> vec(knumints); std::generate(vec.begin(), vec.end(), rand); std::sort(vec.begin(), vec.end()); std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(cout, "\n"));

Sequence Containers

Provides access to sequences of elements. Examples: std::vector<t> std::list<t> std::deque<t> Sequence Containers

std::vector<t>

std::vector<t> A vector represents a sequence of elements of any type. You specify the type when using the vector: std::vector<int> vecint; // vector of ints std::vector<string> vecstr; // vector of string std::vector<mystruct> vecstruct; // vector of mystructs std::vector<std::vector<string>> vecofvec; // vector of vector<string>

Summary of std::vector<t> vs Stanford Vector<T> What you want to do Stanford Vector<int> std::vector<int> Create an empty vector Vector<int> v; vector<int> v; Create a vector with n copies of zero Vector<int> v(n); vector<int> v(n); Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k); Add k to the end of the vector v.add(k); v.push_back(k); Clear vector v.clear(); v.clear(); Get the element at index i (verify that i is in bounds) int k = v.get(i); int k = v[i]; int k = v.at(i); Check if the vector is empty if (v.isempty())... if (v.empty())... Replace the element at index i (verify that i is in bounds) v.get(i) = k; v[i] = k; v.at(i) = k;

Summary of std::vector<t> vs Stanford Vector<T> What you want to do Stanford Vector<int> std::vector<int> Create an empty vector Vector<int> v; vector<int> v; Create a vector with n copies of zero Vector<int> v(n); vector<int> v(n); Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k); Add k to the end of the vector v.add(k); v.push_back(k); Clear vector v.clear(); v.clear(); Get the element at index i (verify that i is in bounds) int k = v.get(i); int k = v[i]; int k = v.at(i); Check if the vector is empty if (v.isempty())... if (v.empty())... Replace the element at index i (verify that i is in bounds) v.get(i) = k; v[i] = k; v.at(i) = k;

Summary of std::vector<t> vs Stanford Vector<T> What you want to do Stanford Vector<int> std::vector<int> Create an empty vector Vector<int> v; vector<int> v; Create a vector with n copies of zero Vector<int> v(n); vector<int> v(n); Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k); Add k to the end of the vector v.add(k); v.push_back(k); Clear vector v.clear(); v.clear(); Get the element at index i (verify that i is in bounds) int k = v.get(i); int k = v[i]; int k = v.at(i); Check if the vector is empty if (v.isempty())... if (v.empty())... Replace the element at index i (verify that i is in bounds) v.get(i) = k; v[i] = k; v.at(i) = k;

Summary of std::vector<t> vs Stanford Vector<T> What you want to do Stanford Vector<int> std::vector<int> Create an empty vector Vector<int> v; vector<int> v; Create a vector with n copies of zero Vector<int> v(n); vector<int> v(n); Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k); Add k to the end of the vector v.add(k); v.push_back(k); Clear vector v.clear(); v.clear(); Get the element at index i (verify that i is in bounds) int k = v.get(i); int k = v[i]; int k = v.at(i); Check if the vector is empty if (v.isempty())... if (v.empty())... Replace the element at index i (verify that i is in bounds) v.get(i) = k; v[i] = k; v.at(i) = k;

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture...

std::vector<t> Problem: Write a program that reads a list of integers and finds the median. Vector Median (VecMedian.pro)

Some new stuff there: std::vector<t> const int knumints = ; using vecsz_t = std::vector<int>::size_type; std::sort(vec.begin(), vec.end());

std::vector<t> Some new stuff there: const int knumints = ; This is a promise to the compiler that this variable won t change. using vecsz_t = std::vector<int>::size_type; std::sort(vec.begin(), vec.end());

std::vector<t> Some new stuff there: const int knumints = ; using vecsz_t = std::vector<int>::size_type; std::sort(vec.begin(), vec.end()); This let s us use vecsz_t as an alias/synonym for the type std::vector<int>::size_type;

std::vector<t> Some new stuff there: const int knumints = ; using vecsz_t = std::vector<int>::size_type; std::sort(vec.begin(), vec.end()); This takes a range of the vector and sorts it

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture...

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture... Why these differences?

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture... Why these differences?

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture... Why these differences?

Why the Differences? Why doesn t std::vector bounds check by default? Hint: Remember our discussion of the philosophy of C++ If you write your program correctly, bounds checking will just slow your code down.

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture... Why these differences?

Some Differences - std::vector<t> vs Stanford Vector<T> Get the element at index i without bounds checking // Impossible! int a = x[i]; Change the element at index i without bounds checking // Impossible! x[i] = v; Add an element to the beginning of a vector // Impossible! (or at least slow) // Impossible! (or at least slow) Apply a function to each element in x x.mapall(fn) // We'll talk about this in another lecture... Concatenate vectors v and v2 v += v2; // We'll talk about this in another lecture... Why these differences?

Why is push_front slow? Requires shifting over of the other elements in the vector down one by one (bad). Illustration: Say we have a small vector 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it. vec.push_front(7); 7 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it. vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Need to shift these elements up to make space in the 0th position. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 Now we can insert the new element. 3 0th index 4

Why is push_front slow? Suppose push_front existed and we used it vec.push_front(7); 7 0th index 3 4

Why is push_front slow?... 7 0th index 3 4

Why is push_front slow? Let s get a sense of the difference: Insertion Speed (InsertionSpeed.pro)

The results: Why is push_front slow?

Why is push_front slow? A vector is the prime tool of choice in most applications! Fast Lightweight Intuitive However, we just saw vectors only grow efficiently in one direction. Sometimes it is useful to be able to push_front quickly! C++ has a solution!

std::deque<t>

std::deque<t> A deque (pronounced deck ) is a double ended queue. Can do everything a vector can do and also Unlike a vector, it is possible (and fast) to push_front and pop_front.

std::deque<t> We can see the efficiency of push_front with a std::deque Deque Speed (DequeSpeed.pro)

The results: std::deque<t>

std::deque<t> The results: Same scale as previous graph

std::deque<t> The results: There are the lines!

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: NULL

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: NULL 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_front(7); NULL 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_front(7); NULL 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_front(7); NULL 7 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: NULL 7 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(3); NULL 7 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(3); NULL 7 3 2 4 9 6

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(3); NULL 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: NULL 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(); NULL 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(); 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(); 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(); 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: deq.push_back(); 7 3 2 4 9 6 3

How does std::deque<t> work? There is no single specific implementation of a deque, but one common one might look like this: 7 3 2 4 9 6 3

Wait a minute...

Question If deque can do everything a vector can do and also has a fast push_front... Why use a vector at all?

Downsides of std::deque<t> Deques support fast push_front operations. However, for other common operations like element access, vector will always outperform a deque. Vector vs Deque (VecDeqSpeed.pro)

The results: Downsides of std::deque<t>

Which to Use? vector is the type of sequence that should be used by default... deque is the data structure of choice when most insertions and deletions take place at the beginning or at the end of the sequence. - C++ ISO Standard (section 23...2):

Questions

Container Adapters

Recall stacks and queues: Container Adapters 3 4 2 stack

Container Adapters Recall stacks and queues: push 3 4 2 stack

Recall stacks and queues: Container Adapters 3 4 2 stack

Container Adapters Recall stacks and queues: pop 3 4 2 stack

Recall stacks and queues: Container Adapters 3 4 2 stack

Container Adapters Recall stacks and queues: 9 6 3 4 back 2 stack queue

Container Adapters Recall stacks and queues: 9 6 3 4 back 2 stack push_back queue

Container Adapters Recall stacks and queues: 9 6 3 4 back 2 stack queue

Container Adapters Recall stacks and queues: pop_front 6 3 4 back 2 stack queue

Container Adapters Recall stacks and queues: 6 3 4 back 2 stack queue

Container Adapters How can we implement stack and queue using the containers we have? Stack: Just limit the functionality of a vector/deque to only allow push_back and pop_back. Queue: Just limit the functionality of a deque to only allow push_back and pop_front. Plus only allow access to top element

Container Adapters For this reason, stacks and queues are known as container adapters.

Container Adapters For this reason, stacks and queues are known as container adapters.

Container Adapters For this reason, stacks and queues are known as container adapters.

Next Time Associative Containers and Iterators