The Standard Template Library. EECS 211 Winter 2018

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

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

Exercise 9 Pointers, Iterators and Recursion

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

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

List, Stack, and Queues

Item 3: Predicates, Part 2: Matters of State

use static size for this buffer

PENN STATE UNIVERSITY Department of Economics

Lecture on pointers, references, and arrays and vectors

19. Dynamic Data Structures II

COMP6771 Advanced C++ Programming

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Dynamic Data Structures

Intermediate Programming, Spring 2017*

MODULE 33 --THE STL-- ALGORITHM PART I

CS11 Advanced C++ Spring 2018 Lecture 2

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

15. Pointers, Algorithms, Iterators and Containers II

MODULE 35 --THE STL-- ALGORITHM PART III

Function Templates. Consider the following function:

TDDD38 - Advanced programming in C++

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

14. Pointers, Algorithms, Iterators and Containers II

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

Containers in C++ and Java

Sequential Containers. Ali Malik

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

CSI33 Data Structures

Object-Oriented Programming

G52CPP C++ Programming Lecture 18

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Practice question Answers

CS197c: Programming in C++

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

Sequential Containers. Ali Malik

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

STL: C++ Standard Library

C++ Objects Overloading Other C++ Peter Kristensen

Expansion statements. Version history. Introduction. Basic usage

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

Linked data structures. EECS 211 Winter 2019

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

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

COEN244: Class & function templates

Part XII. More programming comments. Philip Blakely (LSC) C++ Introduction 349 / 370

Templates and Vectors

Modern C++ (less == more) (more == more)!

Abstract Data Types 1

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

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Introduction to C++ Day 2

Working Draft, Technical Specification for C++ Extensions for Parallelism

Module 9. Templates & STL

Due Date: See Blackboard

Data Structures CSci 1200 Test 2 Questions

How the Adapters and Binders Work

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

STL Quick Reference for CS 241

Associative Containers and Iterators. Ali Malik

COMP6771 Advanced C++ Programming

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

Advanced C ++ Philip Blakely. Laboratory for Scientific Computing, University of Cambridge. Philip Blakely (LSC) Advanced C++ 1 / 119

Abstract Data Types 1

C and C++ 8. Standard Template Library (STL) Stephen Clark

CSE 333 Final Exam 3/19/14

Structuur van Computerprogramma s 2

12. Dictionaries. Dictionary. Idea. Other idea

ICOM 4035 Data Structures. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

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

CS11 Advanced C++ Fall Lecture 1

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

EL2310 Scientific Programming

PHY4321 Summary Notes

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

CS

TDDD38 - Advanced programming in C++

CSE 333 Final Exam Sample Solution 3/19/14

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Standard Template Library

ANSWER: // As long as the 2nd iterator is in the list while (it2!= lst.end()) { if (*it2 < *it1) // not in the sorted order

Writing Generic Functions. Lecture 20 Hartmut Kaiser hkaiser/fall_2013/csc1254.html

The Standard Template Library. An introduction

Priority Queues and Huffman Trees

Scientific programming (in C++)

Botet C++ generic overload functions P0051

Chapter 17: Linked Lists

The C++ Object Lifecycle. EECS 211 Winter 2019

September 19,

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

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

A Parallel Algorithms Library N3724

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

Database Systems on Modern CPU Architectures

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

The Cut and Thrust of CUDA

Distributed and Parallel Systems Group University of Innsbruck. Simone Pellegrini, Radu Prodan and Thomas Fahringer

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Transcription:

The Standard Template Library EECS 211 Winter 2018

2 Problem: finding the maximum element of a vector A simple fixed-size vector struct: struct Int_vec int* data; size_t size; ;

3 Solution: max_int_vec // Finds the index of the maximum element in vec. // - If vec is empty returns 0. // - If the maximum element repeats, returns the first occurrence. size_t max_int_vec(const Int_vec& vec) size_t best = 0; for (size_t i = 1; i < vec.size; ++i) if (vec.data[ best ] < vec.data[ i ]) best = i; return best;

4 Testing max_int_vec TEST_CASE("max(Int_vec)") int data[ ] = 2, 0, 5, 3, 9, 5, 1 ; Int_vec vdata, 7; CHECK( 4 == max_int_vec(v) );

5 Problem: finding the maximum element of a linked list A simple linked list: struct Int_node int data; std::shared_ptr<int_node> next; ;

5 Problem: finding the maximum element of a linked list A simple linked list: struct Int_node int data; std::shared_ptr<int_node> next; ; using Int_list = std::shared_ptr<int_node>;

Problem: finding the maximum element of a linked list A simple linked list: struct Int_node int data; std::shared_ptr<int_node> next; ; using Int_list = std::shared_ptr<int_node>; Int_list cons(int data, Int_list next) Int_list result = std::make_shared<int_node>(); result data = data; result next = next; return result; 5

6 Solution: max_int_list // Finds the link to the node containing the maximum element. // - If the list is empty, returns the null pointer. // - If the maximum repeats, returns the first occurrence. Int_list max_int_list(int_list lst) Int_list best = lst; for (Int_list i = lst; i!= nullptr; i = i next) if (best data < i data) best = i; return best;

7 Testing max_int_list TEST_CASE("max(Int_list)") Int_list expected = cons(9, cons(5, cons(1, nullptr))); Int_list lst = cons(2, cons(0, cons(5, cons(3, expected)))); CHECK( expected == max_int_list(lst) );

8 Making our code more general To make our code more general (and thus more reusable): Make the data structures generic over the element types Make the algorithm generic over the data structures

9 Generic fixed-size vector template <typename T> struct Vec T* data; size_t size; ;

10 Generic max_vec template <typename T> size_t max_vec(const Vec<T>& vec) size_t best = 0; for (size_t i = 1; i < vec.size; ++i) if (vec.data[ best ] < vec.data[ i ]) best = i; return best;

11 Generic linked list template <typename T> struct Node T data; std::shared_ptr<node<t» next; ;

Generic linked list template <typename T> struct Node T data; std::shared_ptr<node<t» next; ; template <typename T> using List = std::shared_ptr<node<t»; template <typename T> List<T> cons(const T& data, List<T> next) List<T> result = std::make_shared<node<t>>(); result data = data; result next = next; return result; 11

12 Generic max_list template <typename T> List<T> max_list(list<t> lst) List<T> best = lst; for (List<T> i = lst; i!= nullptr; i = i next) if (best data < i data) best = i; return best;

13 Introducing the Standard Template Library Includes containers like std::vector<t>, std::list<t> (a doubly-linked list), and more Containers have iterators for traversing them An iterator is like a pointer to one element of a container

14 Vector iterators Like pointers to vector elements

14 Vector iterators Like pointers to vector elements v.begin() returns an iterator to the beginning of v

14 Vector iterators Like pointers to vector elements v.begin() returns an iterator to the beginning of v v.end() returns an iterator to one past the end of v

14 Vector iterators Like pointers to vector elements v.begin() returns an iterator to the beginning of v v.end() returns an iterator to one past the end of v *i dereferences an iterator i

14 Vector iterators Like pointers to vector elements v.begin() returns an iterator to the beginning of v v.end() returns an iterator to one past the end of v *i dereferences an iterator i ++i advances an iterator i to the next element

14 Vector iterators Like pointers to vector elements v.begin() returns an iterator to the beginning of v v.end() returns an iterator to one past the end of v *i dereferences an iterator i ++i advances an iterator i to the next element (and more )

15 List iterators Like pointers to list elements

15 List iterators Like pointers to list elements lst.begin() returns an iterator to the beginning of lst

15 List iterators Like pointers to list elements lst.begin() returns an iterator to the beginning of lst lst.end() returns an iterator to one past the end of lst

15 List iterators Like pointers to list elements lst.begin() returns an iterator to the beginning of lst lst.end() returns an iterator to one past the end of lst *i dereferences an iterator i

15 List iterators Like pointers to list elements lst.begin() returns an iterator to the beginning of lst lst.end() returns an iterator to one past the end of lst *i dereferences an iterator i ++i advances an iterator i to the next element

15 List iterators Like pointers to list elements lst.begin() returns an iterator to the beginning of lst lst.end() returns an iterator to one past the end of lst *i dereferences an iterator i ++i advances an iterator i to the next element (and more )

16 max_vec using std::vector iterators #include <vector> using vector_int_iter = typename std::vector<int>::iterator; vector_int_iter max_vec(std::vector<int>& vec) vector_int_iter best = vec.begin(); for (vector_int_iter i = vec.begin(); i!= vec.end(); ++i) if ( best < i) best = i; return best;

17 max_vec using auto #include <vector> typename std::vector<int>::iterator max_vec(std::vector<int>& vec) auto best = vec.begin(); for (auto i = vec.begin(); i!= vec.end(); ++i) if ( best < i) best = i; return best;

18 max_list using std::list iterators #include <list> typename std::list<int>::iterator max_list(std::list<int>& lst) auto best = lst.begin(); for (auto i = lst.begin(); i!= lst.end(); ++i) if ( best < i) best = i; return best;

19 Making the algorithm generic max_vec and max_list are the same! except for the iterator type

19 Making the algorithm generic max_vec and max_list are the same! except for the iterator type We can use a template to abstract over the iterator type

19 Making the algorithm generic max_vec and max_list are the same! except for the iterator type We can use a template to abstract over the iterator type We ll make the function take an iterator range to search through

20 Generic maximum element algorithm template <typename Fwd_iter> Fwd_iter max_generic(fwd_iter start, Fwd_iter limit) Fwd_iter best = start; for (Fwd_iter i = start; i!= limit; ++i) if ( best < i) best = i; return best;

21 max_generic is very generic It doesn t care about: the shape of the data structure the element type of the data structure whether the iterator is const or not

21 max_generic is very generic It doesn t care about: the shape of the data structure the element type of the data structure whether the iterator is const or not What it does care about: Fwd_iter is copyable (best = i), pre-incrementable (++i), and dereferenceable ( i) The results of dereferencing Fwd_iter are comparable with operator<

22 Using max_generic TEST_CASE("max(vector<int>)") std::vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; auto exp = vec.begin() + 4; CHECK( exp == max_generic(vec.begin(), vec.end()) ); TEST_CASE("max(list<double>") std::list<double> lst 2, 0, 5, 3, 9, 5, 1 ; auto exp = lst.begin(); advance(exp, 4); CHECK( exp == max_generic(lst.begin(), lst.end()) );

23 It s in <algorithm> TEST_CASE("max_element(vector<int>)") std::vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; auto exp = vec.begin() + 4; CHECK( exp == std::max_element(vec.begin(), vec.end()) ); TEST_CASE("max_element(list<double>)") std::list<double> lst 2, 0, 5, 3, 9, 5, 1 ; auto exp = lst.begin(); advance(exp, 4); CHECK( exp == std::max_element(lst.begin(), lst.end()) );

24 STL algorithms The STL <algorithm> header contains many algorithms: http://en.cppreference.com/w/cpp/algorithm

24 STL algorithms The STL <algorithm> header contains many algorithms: http://en.cppreference.com/w/cpp/algorithm Let s try using it for counting

25 Counting occurrences #include <algorithm> using namespace std; const vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; TEST_CASE("count") CHECK( 0 == count(vec.begin(), vec.end(), 4) ); CHECK( 1 == count(vec.begin(), vec.end(), 3) ); CHECK( 2 == count(vec.begin(), vec.end(), 5) );

26 Counting with a predicate bool lt6(int x) return x < 6; const vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; TEST_CASE("count_if(lt6)") CHECK( 6 == count_if(vec.begin(), vec.end(), lt6) );

27 Counting with a function object class Less_than int value_; public: Less_than(int value) : value_(value) bool operator()(int x) const return x < value_; ;

27 Counting with a function object class Less_than int value_; public: Less_than(int value) : value_(value) bool operator()(int x) const return x < value_; ; TEST_CASE("Less_than") Less_than lt(5); CHECK( lt(4) ); CHECK_FALSE( lt(5) );

28 Counting with a function object class Less_than int value_; public: Less_than(int value) : value_(value) bool operator()(int x) const return x < value_; ; const vector<int> v 2, 0, 5, 3, 9, 5, 1 ; CHECK( 6 == count_if(v.begin(), v.end(), Less_than(6)) ); CHECK( 4 == count_if(v.begin(), v.end(), Less_than(5)) );

29 Constructing a function object using std::bind using namespace std::placeholders; const vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; CHECK( 6 == count_if(vec.begin(), vec.end(), std::bind(std::less<int>(), _1, 6)) );

30 The slickest way: lambda const vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; CHECK( 6 == count_if(vec.begin(), vec.end(), [ ](auto x) return x < 6; ) );

30 The slickest way: lambda const vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; CHECK( 6 == count_if(vec.begin(), vec.end(), [ ](auto x) return x < 6; ) ); int y = 5; CHECK( 4 == count_if(vec.begin(), vec.end(), [&](auto x) return x < y; ) );

30 The slickest way: lambda const vector<int> vec 2, 0, 5, 3, 9, 5, 1 ; CHECK( 6 == count_if(vec.begin(), vec.end(), [ ](auto x) return x < 6; ) ); int y = 5; CHECK( 4 == count_if(vec.begin(), vec.end(), [&](auto x) return x < y; ) ); int z = 4; CHECK( 4 == count_if(vec.begin(), vec.end(), [=](auto x) return x < z; ) );

Next: tokenize using STL and iterators (in CLion) 31