Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 121:509-512 Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1.

Similar presentations


Presentation on theme: "CSCE 121:509-512 Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1."— Presentation transcript:

1 CSCE 121:509-512 Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1

2 CSCE 121:509-512 Set 19: The STL: Containers and Iterators The STL STL = Standard Template Library Part of the C++ standard library Extensible framework for dealing with data in C++ programs Key notions of sequence and iterator tie together data and algorithms 2

3 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Common Programming Tasks Collect multiple data items into a container Organize the data according to some rule Retrieve data items – by index: e.g., get the i-th item – by value: e.g., get the first item with the value “Chocolate” – by properties: e.g., get the first item with age < 64 Add new data items Remove existing data items Sorting and searching Simple numeric operations (e.g., add them all up) 3

4 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Key Observation Most of the work to do these programming tasks is independent of the actual type of the data types or even how the data is stored! E.g., for sorting, we just need a way to compare the data items – numeric types, use < – strings, use lexicographic (alphabetical) order E.g., arrays and linked lists have a lot in common 4

5 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Ideal Write code for common programming tasks that does not have to be rewritten every time we come up with a different way to store the data E.g., searching in a vector vs. an array vs. a linked list is not all that different conceptually That is, we want uniform access to the data – independent of how it is stored – independent of its type And be easy to read, easy to modify, fast,… 5

6 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Generic Programming Generalize algorithms – in addition to generalizing data structures Advantages: – increased correctness – greater range of uses (and reuses) – better performance (through tuned libraries) 6

7 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Sum Function for Array double sum(double array[], int n) // assume array is of size n { double s = 0; for (int i = 0; i < n; ++i) s = s + array[i]; return s; } 7

8 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Sum Function for Linked List struct Node { Node* next; int data; }; int sum(Node* first) { int s = 0; while (first) { s+= first->data; first = first->next; } return s; } 8

9 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Pseudocode Version of Both Functions int sum(data) { int s = 0; while (not at end) { // operation 1 s = s + get value; // operation 2 get next data element; // operation 3 } return s; } I.e., initialize sum to 0, loop through all the elements adding them to the sum, and return the sum. 9

10 CSCE 121:509-512 Set 19: The STL: Containers and Iterators STL-Style Version of Generalized Sum Function // Iter must be an “Input_iterator” // T must be a type that can be added and assigned template // first and last refer to data elements; // s accumulates the sum T sum(Iter first, Iter last, T s) { while (first != last) { // operation 1 s = s + *first; // operation 2 ++first; // operation 3 } return s; } 10

11 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Using STL-Style Sum Function float a[] = {1,2,3,4,5,6,7,8}; double d = 0; d = sum(a,a+sizeof(a)/sizeof(*a),d); First initialize the array a. Then initialize the accumulator d. Then call the templated function sum: – Iter is replaced with double* – T is replaced with double First argument is a pointer to the first element of a. Second argument is a pointer to just after the last element of a (why?). 11

12 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Instantiated Sum Function double sum(double* first, double* last, double s) { while (first != last) { s = s + *first; ++first; } return s; } 12

13 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Sum Function Example Almost the standard library accumulate() Works for arrays, vectors, lists, istreams,… Runs as fast as “hand-crafted” code Code’s requirements on the data are made explicit 13

14 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Basic STL Model 14 Algorithms sort, find, search, copy, … Containers vector, list, map, unordered_map, … iterators Separation of concerns –Algorithms manipulate data, but don’t know about containers –Containers store data, but don’t know about algorithms –Algorithms and containers interact through iterators Each container has its own iterator types

15 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Iterators A pair of iterators defines a sequence – the beginning (points to first element, if any) – the end (points to the one-beyond-the-last element) 15 … begin:end:

16 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Iterators An iterator is a type that supports the “iterator operations”: – Go to next element: ++ – Get value: * – Check if two iterators point to same element: == Frequently a pointer type, but not necessarily Some iterators support more operations (--, +, [ ] ) 16 … begin:end:

17 CSCE 121:509-512 Set 19: The STL: Containers and Iterators One-Past-The-Last An iterator point to (refers to, denotes) an element of a sequence The end of the sequence is “one past the last element”, not the last element! Reason is to elegantly represent an empty sequence One-past-the last element is not an element – you can compare an iterator pointing to it – but you cannot dereference it (get its value) 17 0123 the end: An empty sequence: begin: end: some iterator:

18 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Containers Hold sequences in different ways vector 18 0123

19 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Containers Hold sequences in different ways list (doubly-linked) 19 01 2

20 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Containers Hold sequences in different ways set (a kind of tree) 20 10 6 2 5 7 34

21 CSCE 121:509-512 Set 19: The STL: Containers and Iterators STL find() algorithm // find first element that equals a value template In find(In first, In last, const T& val) { while (first != last && *first != val) ++first; return first; } // sample use of find, for vector void f(vector & v, int x) { vector ::iterator p = find(v.begin(),v.end(),x); if (p != v.end()) { /* we found x */} //... } 21

22 CSCE 121:509-512 Set 19: The STL: Containers and Iterators More About STL find() Function It is generic in two aspects – element type (sequence of any kind of data) – container type (is the sequence implemented with an array, a vector, a linked list, …?) The next slide shows how similar the code is between – a vector of ints – a list of strings – a set of doubles 22

23 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Comparing Different Uses of find() void f(vector & v, int x) { vector ::iterator p = find(v.begin(),v.end(),x); if (p!= v.end()) { /*... */ } } void f(list & v, string x) { list ::iterator p = find(v.begin(),v.end(),x); if (p != v.end()) { /*... */ } } void f(set & v, double x) { set ::iterator p = find(v.begin,v.end(),x); if (p != v.end()) { /*... */ } } 23

24 CSCE 121:509-512 Set 19: The STL: Containers and Iterators find_if() Algorithm Design Find the first element in the container that matches a criterion Criterion is also general: let user specify what it should be – e.g., first element that is odd To do this, pass in as an argument a predicate – function that returns either true or false – e.g., whether or not current element is odd 24

25 CSCE 121:509-512 Set 19: The STL: Containers and Iterators find_if() Algorithm Code template In find_if(In first, In last, Pred pred) { while (first != last && !pred(*first)) ++first; return first; } // instantiating find_if void f(vector & v) { vector ::iterator p = find_if(v.begin(),v.end(),odd); // odd? if (p != v.end()) { /* we found an odd number */ } } 25

26 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Predicate Function A function that returns a bool: bool odd(int i) { return i%2; // % is remainder operator; // 0 = false, non-zero = true } We pass in a function as an argument to find_if(), like we saw for plotting functions 26

27 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Limitations of Passing a Function Sometimes just passing a function as a parameter isn’t good enough Suppose we need to also pass in a parameter to the predicate function – Example: find first element that is less than some target value – the less-than function is the predicate – but we also need to pass in the target value (not fixed in advance) 27

28 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Defining Predicate Function Object Make a class that compares an object of any type against any value (note genericity) We can store state in the class’ object template struct Less_than { T val; // value to compare against Less_than(T& x) : val(x) { } // constructor // overload function call operator: bool operator()(const T& x) const { return x < val; } }; 28

29 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Overloading Function Call Operator Suppose you’ve made an object of type Less_than, call it lt. – The constructor sets the data member “val” equal to the constructor’s argument. You can then say “lt(x)”, which invokes Less_than::operator() for object lt – this function returns true if x is less than val, and false otherwise. It is analogous to the way v[i] is shorthand for vector::operator[]() 29

30 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Using Predicate Function Object template struct Less_than { T val; // value to compare against Less_than(T& x) : val(x) { } // constructor // overload function call operator: bool operator()(const T& x) const { return x < val; } }; // find x < 43 in a vector of ints: auto p = find_if(v.begin(),v.end(),Less_than(43)); // find x < “perfection” in a list of strings: auto q = find_if(ls.begin(),ls.end(),Less_than(“perfection”)); 30

31 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Function Objects Very efficient technique – faster than equivalent function, even assuming there is an equivalent function Main method of “policy parameterization” in the STL Key to emulating functional programming techniques in C++ (cf. CSCE 314) 31

32 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Policy Parameterization For example, we need to parameterize sort() by the comparison criterion struct Record { string name; // standard string for ease of use char addr[24]; // old C-style string to match database //... }; vector vr; //... sort(vr.begin(),vr.end(),Cmp_byname()); // sort by name sort(vr.begin(),vr.end(),Cmp_byaddr()); // sort by addr 32

33 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Comparison for Name struct Cmp_by_name{ // overload function call operator: bool operator()(const Record& a, const Record& b) const { return a.name < b.name; // use name field } }; // uses < which is defined for standard // library string 33

34 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Comparison for Address struct Cmp_by_addr { // overload function call operator: bool operator()(const Record& a, const Record& b) const { return 0 < strncmp(a.addr,b.addr,24); // correct? } }; // comparison function (objects) hide ugly // and error-prone code 34

35 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Defining Policies with Lambda Expressions Lambda expressions let you write short functions “in line” without having to name the functions Usually to pass as a parameter to another function Syntax is to put “[ ]” where the return type and name usually go 35

36 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Defining Policies with Lambda Expressions vector vr; //... sort(vr.begin(),vr.end(), [](const Record& a, const Record& b) { return a.name < b.name; } // sort by name ); sort(vr.begin(),vr.end(), [](const Record& a, const Record& b) { return 0 < strncmp(a.addr,b.addr,24); } // sort by address ); 36

37 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Policy Parameterization Guidelines Use a named function object as argument – to do something complicated – if you want to do the same in several places Use a lambda expression as argument – if what you want is short and obvious Decide based on clarity of code – there are no performance differences between function objects and lambdas – both tend to be faster than function arguments 37

38 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Vector and List Implementations Let’s peek inside vector and list implementation focusing on iterators and more functions Topics: – what type is a vector/list iterator? – begin and end functions – erase function (remove an element) – insert function (add a function anywhere) 38

39 CSCE 121:509-512 Set 19: The STL: Containers and Iterators vector Implementation template class vector { T* elements; //... using iterator = ???; // type of an iterator is implementation-defined // and it varies; a vector iterator could be a // pointer to an element. “using” is the same as // typedef – gives convenient name to the type iterator begin(); // points to first element iterator end(); // points to one past last element iterator erase(iterator p); // remove element pointed to by p iterator insert(iterator p, const T& v); // insert new element v before element pointed to by p }; 39

40 CSCE 121:509-512 Set 19: The STL: Containers and Iterators insert() into vector Shift down all the array elements after the indicated location and then put new element in that location Warning! If you have other iterators that refer to elements of the vector, they are now invalid – some, and perhaps all, of the elements have moved so their addresses have changed 40

41 CSCE 121:509-512 Set 19: The STL: Containers and Iterators vector insert() Example vector ::iterator p = v.begin(); ++p; ++p; ++p; vector ::iterator q = p; ++q; p = v.insert(p,99); 41 6 021345 v: p: q: 7 0219934 v: p: 5 q:

42 CSCE 121:509-512 Set 19: The STL: Containers and Iterators erase() from vector Shift up all the array elements starting at the indicated location to close up the “hole” Warning! If you have other iterators that refer to elements of the vector, they are now invalid – some, and perhaps all, of the elements have moved so their addresses have changed 42

43 CSCE 121:509-512 Set 19: The STL: Containers and Iterators vector erase() Example Continuing the previous example: p = v.erase(p); 43 7 0219934 v: p: 5 q: 6 021345 v: p: q:

44 CSCE 121:509-512 Set 19: The STL: Containers and Iterators list Implementation template class list { Link* elements; //... using iterator = ???; // type of an iterator is implementation-defined // and it varies; a list iterator could be a // pointer to a link node. “using” is the same as // typedef – gives convenient name to the type iterator begin(); // points to first element iterator end(); // points to one past last element iterator erase(iterator p); // remove element pointed to by p iterator insert(iterator p, const T& v); // insert new element v before element pointed to by p }; 44 T value Link* prev Link* succ Link:

45 CSCE 121:509-512 Set 19: The STL: Containers and Iterators insert() and erase() for list Since we have a linked list, we can insert and erase from the list without disturbing the locations of the other elements Manipulate the pointers in the Link nodes appropriately to splice in or out the relevant Link node Other iterators that point to list elements are NOT invalidated for the list container 45

46 CSCE 121:509-512 Set 19: The STL: Containers and Iterators list insert() Example list ::iterator p = v.begin(); ++p; ++p; ++p; list ::iterator q = p; ++q; v = v.insert(p,99); 46 6 0 2134 5 v: p: q: 7 021345 v: p: 99 q:

47 CSCE 121:509-512 Set 19: The STL: Containers and Iterators list erase() Example p = v.erase(p); 47 7 021345 v: p: 99 q: 6 0 2 1 34 5 v: p: q:

48 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Ways of Traversing a Vector for (int i = 0; i < v.size(); ++i) // why int? // do something with v[i] for (vector ::size_type i = 0; i < v.size(); ++i) // do something with v[i] // longer but always correct for (vector ::iterator p = v.begin(); p != v.end(); ++p) // do something with *p 48

49 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Subscript vs. Iterator Vector Traversals subscript style is used in essentially every language iterator style is used in C (pointers only) and C++ iterator style is used for standard library algorithms subscript style does not work for lists (in C++ and in most languages) iterator style works for all containers use subscript style if you need to know the index prefer size_type over plain int (pendantic but quiets compiler and prevents rare errors) 49

50 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Another Way of Traversing a Vector: Range For for (vector ::value_type x : v) // do something with x; // assumes vector implementation includes // “using value_type T;” for (auto& x : v) // do something with x Use “range for” in this situation: – very simple loop – over a single sequence – you don’t need to access more than one element at a time – you don’t need to know the position of the current element 50

51 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Vector vs. List By default, use vector – provides push_back, insert, erase and more – elements are stored compactly and contiguously If you don’t want elements to move, use a list – provides push_back, push_front, insert, erase and more – elements are independently allocated More containers coming up in next lecture... 51

52 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Some Useful Standard Headers sort, copy,... accumulate,... function objects,, coming up 52

53 CSCE 121:509-512 Set 19: The STL: Containers and Iterators More on the STL Designed by Alex Stepanov Goal: most general, efficient, and flexible representation of concepts (algorithms – represent separate concepts with separate code – combine concepts Make programming “like math” 53

54 CSCE 121:509-512 Set 19: The STL: Containers and Iterators STL by the Numbers ISO C++ standard framework has about 10 containers and about 60 algorithms connected by iterators Other organizations provide more containers and algorithms in the style of the STL – Boost.org, Microsoft, SGI,… Probably the currently best known and most widely used example of generic programming 54

55 CSCE 121:509-512 Set 19: The STL: Containers and Iterators STL References If you know the basic concepts and a few examples, you can use the rest Documentation – SGI http://www.sgi.com/tech/stl/ (recommended because of clarity) – Dinkumware http://www.dinkumware.com/refxcpp.html (beware of several library versions) – Rogue Wave http://www.roguewave.com/support/docs/sourcepro/stdlibug/ind ex.html More accessible and less complete documentation – Appendix B 55

56 CSCE 121:509-512 Set 19: The STL: Containers and Iterators Acknowledgments Photo on title slide: Dakar Baskets by Carrie Cizauskas, license CC BY-NC-ND 2.0Dakar Baskets Carrie CizauskasCC BY-NC-ND 2.0 Slides are based on those for the textbook: http://www.stroustrup.com/Programming/20_vector.ppt 56


Download ppt "CSCE 121:509-512 Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1."

Similar presentations


Ads by Google