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

COMP6771 Advanced C++ Programming

STL: C++ Standard Library

Computer Science II Lecture 2 Strings, Vectors and Recursion

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

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

use static size for this buffer

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

Chapter 5. The Standard Template Library.

Chapter 17: Linked Lists

Standard Template Library

SSE2034: System Software Experiment 3

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

TDDD38 - Advanced programming in C++

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

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

STL Quick Reference for CS 241

Stacks and their Applications

Chapter 18: Stacks And Queues

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

Chapter 17: Linked Lists

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

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

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

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

Working with Batches of Data

COMP6771 Advanced C++ Programming

Containers in C++ and Java

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

19. Dynamic Data Structures II

Streams. Ali Malik

The Standard Template Library. EECS 211 Winter 2018

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

STL Containers, Part I

Concurrent Data Structures in C++ CSInParallel Project

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

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

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

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

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

CS24 Week 4 Lecture 2

Patterns: Working with Arrays

Introduction to Linked List: Review. Source:

CS24 Week 3 Lecture 1

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

CS 103 Unit 11. Linked Lists. Mark Redekopp

Function Templates. Consider the following function:

Computational Physics

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

1. The term STL stands for?

Lesson 13 - Vectors Dynamic Data Storage

CS 103 Unit 12 Slides

PIC 10A. Lecture 23: Intro to STL containers

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

Templates and Vectors

TEMPLATES AND ITERATORS

Programming with Haiku

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Priority Queues and Huffman Trees

Biostatistics 615/815 Lecture 6: Linear Sorting Algorithms and Elementary Data Structures

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Exercise 6.2 A generic container class

Prefix Trees Tables in Practice

Chapter 18: Stacks And Queues

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

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

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

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

Introduction to C++ Introduction to C++ 1

September 19,

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

STL. Zoltán Porkoláb: C++11/14 1

Part X. Advanced C ++

The Standard Template Library. An introduction

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

PHY4321 Summary Notes

Advanced Associative Containers. Ali Malik

LECTURE 11 TREE TRAVERSALS

Linked lists Tutorial 5b

Lectures 11,12. Online documentation & links

Memory and Pointers written by Cathy Saxton

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

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

Expression Trees and the Heap

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

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS 103 Unit 11. Linked Lists. Mark Redekopp

Advanced C++ STL. Tony Wong

Cours de C++ Introduction

Functions. Ali Malik

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Transcription:

Sequential Containers Ali Malik malikali@stanford.edu

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

Announcements

Recap

stringstream

Sometimes we want to be able to treat a string like a stream. Useful scenarios: stringstream Converting between data types Tokenizing a string

stringstream #include <sstream> std::string line = "137 2.718 Hello"; std::stringstream stream(line); int myint; double mydouble; std::string mystring; stream >> myint >> mydouble >> mystring; std::cout << myint << std::endl; std::cout << mydouble << std::endl; std::cout << mystring << std::endl;

stringstream Let s write some of the Stanford simpio library! Simple IO (OurSimpIO.pro)

Useful Aside

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

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

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 Functors/Lambdas Iterators Adapters Allocators Containers

Overview of STL Algorithms Functors/Lambdas Iterators Adapters Allocators Containers

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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 = 5; using vecsz_t = std::vector<int>::size_type; std::sort(vec.begin(), vec.end());

std::vector<t> Some new stuff there: const int knumints = 5; 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());

Some new stuff there: std::vector<t> const int knumints = 5; 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;

Some new stuff there: std::vector<t> const int knumints = 5; 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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 Change the element at index i without bounds checking // Impossible! int a = x[i]; // Impossible! x[i] = v; Add an element to the beginning of a vector Apply a function to each element in x // Impossible! (or at least slow) x.mapall(fn) // Impossible! (or at least slow) // We'll talk about this in another lecture... Concatenate vectors v1 and v2 v1 += 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 1 4 1 5 0th index

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 1 2 6 5 4 1 5 9

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 1 2 6 5 4 1 5 9

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 1 2 6 5 4 1 5 9

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 1 2 6 5 4 1 5 9

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 1 2 6 5 4 1 5 9

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 1 2 6 5 4 1 5 9

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 1 2 6 5 4 1 5 9

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 1 2 6 5 3 4 1 5 9

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 1 2 6 5 3 4 1 5 9

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(5); NULL 7 3 1 2 6 5 3 4 1 5 9

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(5); 7 3 1 2 6 5 3 4 1 5 9

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(5); 7 3 1 2 6 5 3 4 1 5 9

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(5); 7 3 1 2 6 5 3 4 1 5 9

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(5); 5 7 3 1 2 6 5 3 4 1 5 9

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

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.1.1.2):

Questions

Container Adapters

Recall stacks and queues: Container Adapters 13 41 12 stack

Recall stacks and queues: Container Adapters push 5 13 41 12 stack

Recall stacks and queues: Container Adapters 5 13 41 12 stack

Recall stacks and queues: pop Container Adapters 13 41 12 stack

Recall stacks and queues: Container Adapters 13 41 12 stack

Recall stacks and queues: Container Adapters 9 13 41 16 11 12 back stack queue

Recall stacks and queues: Container Adapters 9 13 41 12 stack push_back 16 11 5 queue back

Recall stacks and queues: Container Adapters 9 13 41 12 stack 16 11 5 queue back

Container Adapters Recall stacks and queues: pop_front 13 41 12 stack 16 11 5 queue back

Recall stacks and queues: Container Adapters 13 41 12 stack 16 11 5 queue back

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