Chapter 20 The STL (containers, iterators, and algorithms) Bjarne Stroustrup www.stroustrup.com/Programming.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Chapter 20 The STL (containers, iterators, and algorithms)
Chapter 20 The STL (containers, iterators, and algorithms) John Keyser’s Modifications of Slides by Bjarne Stroustrup
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Hashing as a Dictionary Implementation
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Chapter 10.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Hash Tables1 Part E Hash Tables  
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
C++ for Engineers and Scientists Third Edition
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Object Oriented Data Structures
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
Containers Overview and Class Vector
Chapter 21 The STL (maps and algorithms) John Keyser’s Modifications of Slides by Bjarne Stroustrup
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Defining New Types Lecture 21 Hartmut Kaiser
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Working with Batches of Data Lecture 4 Hartmut Kaiser
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
CSCE 121: Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Using Sequential Containers Lecture 8 Hartmut Kaiser
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
Standard Template Library (STL)
Associative Structures
Chapter 20 The STL (containers, iterators, and algorithms)
Introduction to the Standard Template Library
Standard Template Library Model
STL: Traversing a Vector
Chapter 16 Linked Structures
Introduction to Data Structure
An Introduction to STL.
Standard Template Library
Presentation transcript:

Chapter 20 The STL (containers, iterators, and algorithms) Bjarne Stroustrup

Abstract This lecture and the next present the STL – the containers and algorithms part of the C++ standard library This lecture and the next present the STL – the containers and algorithms part of the C++ standard library The STL is an extensible framework dealing with data in a C++ program. The STL is an extensible framework dealing with data in a C++ program. First, I will present the general ideal, then the fundamental concepts, and finally examples of containers and algorithms. First, I will present the general ideal, then the fundamental concepts, and finally examples of containers and algorithms. The key notions of sequence and iterator used to tie data together with algorithms (for general processing) are also presented. The key notions of sequence and iterator used to tie data together with algorithms (for general processing) are also presented. 2Stroustrup/Programming - Nov'13

Overview Common tasks and ideals Common tasks and ideals Generic programming Generic programming Containers, algorithms, and iterators Containers, algorithms, and iterators The simplest algorithm: find() The simplest algorithm: find() Parameterization of algorithms Parameterization of algorithms find_if() and function objects find_if() and function objects Sequence containers Sequence containers vector and list vector and list Associative containers Associative containers map, set map, set Standard algorithms Standard algorithms copy, sort, … copy, sort, … Input iterators and output iterators Input iterators and output iterators List of useful facilities List of useful facilities Headers, algorithms, containers, function objects Headers, algorithms, containers, function objects 3Stroustrup/Programming - Nov'13

Common tasks Collect data into containers Collect data into containers Organize data Organize data For printing For printing For fast access For fast access Retrieve data items Retrieve data items By index (e.g., get the Nth element) By index (e.g., get the Nth element) By value (e.g., get the first element with the value "Chocolate") By value (e.g., get the first element with the value "Chocolate") By properties (e.g., get the first elements where “age<64”) By properties (e.g., get the first elements where “age<64”) Add data Add data Remove data Remove data Sorting and searching Sorting and searching Simple numeric operations Simple numeric operations 4Stroustrup/Programming - Nov'13

Observation We can (already) write programs that are very similar independent of the data type used Using an int isn’t that different from using a double Using an int isn’t that different from using a double Using a vector isn’t that different from using a vector Using a vector isn’t that different from using a vector 5Stroustrup/Programming - Nov'13

Ideals We’d like to write common programming tasks so that we don’t have to re-do the work each time we find a new way of storing the data or a slightly different way of interpreting the data Finding a value in a vector isn’t all that different from finding a value in a list or an array Finding a value in a vector isn’t all that different from finding a value in a list or an array Looking for a string ignoring case isn’t all that different from looking at a string not ignoring case Looking for a string ignoring case isn’t all that different from looking at a string not ignoring case Graphing experimental data with exact values isn’t all that different from graphing data with rounded values Graphing experimental data with exact values isn’t all that different from graphing data with rounded values Copying a file isn’t all that different from copying a vector Copying a file isn’t all that different from copying a vector 6Stroustrup/Programming - Nov'13

Ideals (continued) Code that’s Code that’s Easy to read Easy to read Easy to modify Easy to modify Regular Regular Short Short Fast Fast Uniform access to data Uniform access to data Independently of how it is stored Independently of how it is stored Independently of its type Independently of its type … 7Stroustrup/Programming - Nov'13

Ideals (continued) … Type-safe access to data Type-safe access to data Easy traversal of data Easy traversal of data Compact storage of data Compact storage of data Fast Fast Retrieval of data Retrieval of data Addition of data Addition of data Deletion of data Deletion of data Standard versions of the most common algorithms Standard versions of the most common algorithms Copy, find, search, sort, sum, … Copy, find, search, sort, sum, … 8Stroustrup/Programming - Nov'13

Examples Sort a vector of strings Sort a vector of strings Find an number in a phone book, given a name Find an number in a phone book, given a name Find the highest temperature Find the highest temperature Find all values larger than 800 Find all values larger than 800 Find the first occurrence of the value 17 Find the first occurrence of the value 17 Sort the telemetry records by unit number Sort the telemetry records by unit number Sort the telemetry records by time stamp Sort the telemetry records by time stamp Find the first value larger than “Petersen”? Find the first value larger than “Petersen”? What is the largest amount seen? What is the largest amount seen? Find the first difference between two sequences Find the first difference between two sequences Compute the pairwise product of the elements of two sequences Compute the pairwise product of the elements of two sequences What are the highest temperatures for each day in a month? What are the highest temperatures for each day in a month? What are the top 10 best-sellers? What are the top 10 best-sellers? What’s the entry for “C++” (say, in Google)? What’s the entry for “C++” (say, in Google)? What’s the sum of the elements? What’s the sum of the elements? 9Stroustrup/Programming - Nov'13

10 Generic programming Generalize algorithms Generalize algorithms Sometimes called “lifting an algorithm” Sometimes called “lifting an algorithm” The aim (for the end user) is The aim (for the end user) is Increased correctness Increased correctness Through better specification Through better specification Greater range of uses Greater range of uses Possibilities for re-use Possibilities for re-use Better performance Better performance Through wider use of tuned libraries Through wider use of tuned libraries Unnecessarily slow code will eventually be thrown away Unnecessarily slow code will eventually be thrown away G o from the concrete to the more abstract G o from the concrete to the more abstract The other way most often leads to bloat The other way most often leads to bloat Stroustrup/Programming - Nov'13

11 Lifting example (concrete algorithms) double sum(double array[], int n)// one concrete algorithm (doubles in array) { double s = 0; for (int i = 0; i < n; ++i ) s += array[i]; return s; } struct Node { Node* next; int data; }; int sum(Node* first)// another concrete algorithm (ints in list) { int s = 0; while (first) {// terminates when expression is false or zero s += first->data; first = first->next; } return s; } Stroustrup/Programming - Nov'13

12 Lifting example (abstract the data structure) // pseudo-code for a more general version of both algorithms int sum(data)// somehow parameterize with the data structure { int s = 0;// initialize while (not at end) {// loop through all elements s += get value; // compute sum s += get value; // compute sum get next data element; get next data element;} return s;// return result } We need three operations (on the data structure): We need three operations (on the data structure): not at end not at end get value get value get next data element get next data element Stroustrup/Programming - Nov'13

13 Lifting example (STL version) // Concrete STL-style code for a more general version of both algorithms template // Iter should be an Input_iterator // T should be something we can + and = T sum(Iter first, Iter last, T s)// T is the “accumulator type” { while (first!=last) { while (first!=last) { s += *first; ++first;} return s; } Let the user initialize the accumulator Let the user initialize the accumulator float a[] = { 1,2,3,4,5,6,7,8 }; double d = 0; d = sum(a,a+sizeof(a)/sizeof(*a),d); Stroustrup/Programming - Nov'13

14 Lifting example Almost the standard library accumulate Almost the standard library accumulate I simplified a bit for terseness (see 21.5 for more generality and more details) I simplified a bit for terseness (see 21.5 for more generality and more details) Works for Works for arrays arrays vectors vectors lists lists istreams istreams … Runs as fast as “hand-crafted” code Runs as fast as “hand-crafted” code Given decent inlining Given decent inlining The code’s requirements on its data has become explicit The code’s requirements on its data has become explicit We understand the code better We understand the code better Stroustrup/Programming - Nov'13

The STL Part of the ISO C++ Standard Library Part of the ISO C++ Standard Library Mostly non-numerical Mostly non-numerical Only 4 standard algorithms specifically do computation Only 4 standard algorithms specifically do computation Accumulate, inner_product, partial_sum, adjacent_difference Accumulate, inner_product, partial_sum, adjacent_difference Handles textual data as well as numeric data Handles textual data as well as numeric data E.g. string E.g. string Deals with organization of code and data Deals with organization of code and data Built-in types, user-defined types, and data structures Built-in types, user-defined types, and data structures Optimizing disk access was among its original uses Optimizing disk access was among its original uses Performance was always a key concern Performance was always a key concern 15Stroustrup/Programming - Nov'13

The STL Designed by Alex Stepanov Designed by Alex Stepanov General aim: The most general, most efficient, most flexible representation of concepts (ideas, algorithms) General aim: The most general, most efficient, most flexible representation of concepts (ideas, algorithms) Represent separate concepts separately in code Combine concepts freely wherever meaningful General aim to make programming “like math” General aim to make programming “like math” or even “Good programming is math” works for integers, for floating-point numbers, for polynomials, for … 16Stroustrup/Programming - Nov'13

Basic model Algorithms Algorithms sort, find, search, copy, … Containers Containers vector, list, map, unordered_map, … 17 iterators Separation of concernsSeparation 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 typesEach container has its own iterator types Stroustrup/Programming - Nov'13

The STL An ISO C++ standard framework of about 10 containers and about 60 algorithms connected by iterators An ISO C++ standard framework of about 10 containers and about 60 algorithms connected by iterators Other organizations provide more containers and algorithms in the style of the STL Other organizations provide more containers and algorithms in the style of the STL Boost.org, Microsoft, SGI, … Boost.org, Microsoft, SGI, … Probably the currently best known and most widely used example of generic programming Probably the currently best known and most widely used example of generic programming 18Stroustrup/Programming - Nov'13

The STL If you know the basic concepts and a few examples you can use the rest If you know the basic concepts and a few examples you can use the rest Documentation Documentation SGI SGI (recommended because of clarity) (recommended because of clarity) Dinkumware Dinkumware (beware of several library versions) (beware of several library versions) Rogue Wave Rogue Wave More accessible and less complete documentation More accessible and less complete documentation Appendix B Appendix B 19Stroustrup/Programming - Nov'13

Basic model A pair of iterators defines a sequence A pair of iterators defines a sequence The beginning (points to the first element – if any) The beginning (points to the first element – if any) The end (points to the one-beyond-the-last element) The end (points to the one-beyond-the-last element) 20 … begin:end: An iterator is a type that supports the “iterator operations” An iterator is a type that supports the “iterator operations” ++ Go to next element ++ Go to next element * Get value * Get value == Does this iterator point to the same element as that iterator? == Does this iterator point to the same element as that iterator? Some iterators support more operations (e.g. --, +, and [ ]) Some iterators support more operations (e.g. --, +, and [ ]) Stroustrup/Programming - Nov'13

Containers (hold sequences in difference ways) vector vector list list (doubly linked) set set (a kind of tree) Stroustrup/Programming - Nov'13

The simplest algorithm: find() // Find the first element that equals a value template template In find(In first, In last, const T& val) { while (first!=last && *first != val) ++first; return first; } void f(vector & v, int x)// find an int in a vector { vector ::iterator p = find(v.begin(),v.end(),x); if (p!=v.end()) { /* we found x */ } // … } 22 … begin: end: We can ignore (“abstract away”) the differences between containers Stroustrup/Programming - Nov'13

find() generic for both element type and container type void f(vector & v, int x)// works for vector of ints { vector ::iterator p = find(v.begin(),v.end(),x); if (p!=v.end()) { /* we found x */ } // … } void f(list & v, string x)// works for list of strings { list ::iterator p = find(v.begin(),v.end(),x); if (p!=v.end()) { /* we found x */ } // … } void f(set & v, double x)// works for set of doubles { set ::iterator p = find(v.begin(),v.end(),x); if (p!=v.end()) { /* we found x */ } // … } 23Stroustrup/Programming - Nov'13

Algorithms and iterators An iterator points to (refers to, denotes) an element of a sequence An iterator points to (refers to, denotes) an element of a sequence The end of the sequence is “one past the last element” The end of the sequence is “one past the last element” not “the last element” not “the last element” That’s necessary to elegantly represent an empty sequence That’s necessary to elegantly represent an empty sequence One-past-the-last-element isn’t an element One-past-the-last-element isn’t an element You can compare an iterator pointing to it You can compare an iterator pointing to it You can’t dereference it (read its value) You can’t dereference it (read its value) Returning the end of the sequence is the standard idiom for “not found” or “unsuccessful” Returning the end of the sequence is the standard idiom for “not found” or “unsuccessful” the end: An empty sequence: begin: end: some iterator: Stroustrup/Programming - Nov'13

Simple algorithm: find_if() Find the first element that matches a criterion (predicate) Find the first element that matches a criterion (predicate) Here, a predicate takes one argument and returns a bool Here, a predicate takes one argument and returns a bool template template In find_if(In first, In last, Pred pred) { while (first!=last && !pred(*first)) ++first; return first; } void f(vector & v) { vector ::iterator p = find_if(v.begin(),v.end,Odd()); if (p!=v.end()) { /* we found an odd number */ } // … } 25 A predicate Stroustrup/Programming - Nov'13

Predicates A predicate (of one argument) is a function or a function object that takes an argument and returns a bool A predicate (of one argument) is a function or a function object that takes an argument and returns a bool For example For example A function A function bool odd(int i) { return i%2; } // % is the remainder (modulo) operator odd(7); // call odd: is 7 odd ? A function object A function object struct Odd { bool operator()(int i) const { return i%2; } }; Odd odd;// make an object odd of type Odd odd(7);// call odd: is 7 odd? 26Stroustrup/Programming - Nov'13

Function objects A concrete example using state A concrete example using state template struct Less_than { T val;// value to compare with Less_than(T& x) :val(x) { } bool operator()(const T& x) const { return x < val; } }; // find x : p=find_if(v.begin(), v.end(), Less_than(43)); // find x : q=find_if(ls.begin(), ls.end(), Less_than("perfection")); 27Stroustrup/Programming - Nov'13

Function objects A very efficient technique A very efficient technique inlining very easy inlining very easy and effective with current compilers and effective with current compilers Faster than equivalent function Faster than equivalent function And sometimes you can’t write an equivalent function And sometimes you can’t write an equivalent function The main method of policy parameterization in the STL The main method of policy parameterization in the STL Key to emulating functional programming techniques in C++ Key to emulating functional programming techniques in C++ 28Stroustrup/Programming - Nov'13

Policy parameterization Whenever you have a useful algorithm, you eventually want to parameterize it by a “policy”. Whenever you have a useful algorithm, you eventually want to parameterize it by a “policy”. For example, we need to parameterize sort by the comparison criteria For example, we need to parameterize sort by the comparison criteria struct Record { string name;// standard string for ease of use char addr[24];// old C-style string to match database layout // … }; vector vr; // … sort(vr.begin(), vr.end(), Cmp_by_name());// sort by name sort(vr.begin(), vr.end(), Cmp_by_addr());// sort by addr 29Stroustrup/Programming - Nov'13

Comparisons // Different comparisons for Rec objects: struct Cmp_by_name { bool operator()(const Rec& a, const Rec& b) const { return a.name < b.name; } // look at the name field of Rec }; struct Cmp_by_addr { bool operator()(const Rec& a, const Rec& b) const { return 0 < strncmp(a.addr, b.addr, 24); }// correct? }; // note how the comparison function objects are used to hide ugly // and error-prone code 30Stroustrup/Programming - Nov'13

Policy parameterization Whenever you have a useful algorithm, you eventually want to parameterize it by a “policy”. Whenever you have a useful algorithm, you eventually want to parameterize it by a “policy”. For example, we need to parameterize sort by the comparison criteria For example, we need to parameterize sort by the comparison criteria vector vr; // … sort(vr.begin(), vr.end(), [] (const Rec& a, const Rec& b) { return a.name < b.name; } // sort by name ); sort(vr.begin(), vr.end(), [] (const Rec& a, const Rec& b) { return 0 < strncmp(a.addr, b.addr, 24); } // sort by addr ); 31Stroustrup/Programming - Nov'13

Policy parameterization Use a named object as argument Use a named object as argument If you want to do something complicated If you want to do something complicated If you feel the need for a comment If you feel the need for a comment If you want to do the same in several places If you want to do the same in several places Use a lambda expression as argument Use a lambda expression as argument If what you want is short and obvious If what you want is short and obvious Choose based on clarity of code Choose based on clarity of code There are no performance differences between function objects and lambdas There are no performance differences between function objects and lambdas Function objects (and lambdas) tend to be faster than function arguments Function objects (and lambdas) tend to be faster than function arguments 32Stroustrup/Programming - Nov'13

vector template class vector { T* elements; // … using value_type = T; using iterator = ???;// the type of an iterator is implementation defined // and it (usefully) varies (e.g. range checked iterators) // a vector iterator could be a pointer to an element using const_iterator = ???; iterator begin();// points to first element const_iterator begin() const; iterator end();// points to one beyond the last element const_iterator end() const; iterator erase(iterator p);// remove element pointed to by p iterator insert(iterator p, const T& v);// insert a new element v before p }; 33Stroustrup/Programming - Nov'13

insert() into vector vector ::iterator p = v.begin(); ++p; ++p; ++p; vector ::iterator q = p; ++q; v: p=v.insert(p,99); // leaves p pointing at the inserted element p: v: p: 5 q:  Note: q is invalid after the insert()  Note: Some elements moved; all elements could have moved Stroustrup/Programming - Nov'13

erase() from vector 35 p = v.erase(p);// leaves p pointing at the element after the erased one  vector elements move when you insert() or erase()  Iterators into a vector are invalidated by insert() and erase() v: p: 5 q: v: p: q: Stroustrup/Programming - Nov'13

list template class list { Link* elements; // … using value_type = T; using iterator = ???;// the type of an iterator is implementation defined // and it (usefully) varies (e.g. range checked iterators) // a list iterator could be a pointer to a link node using const_iterator = ???; iterator begin();// points to first element const_iterator begin() const; iterator end();// points one beyond the last element const_iterator end() const; iterator erase(iterator p);// remove element pointed to by p iterator insert(iterator p, const T& v);// insert a new element v before p }; 36 T value Link* pre Link* post Link: Stroustrup/Programming - Nov'13

insert() into list list ::iterator p = v.begin(); ++p; ++p; ++p; list ::iterator q = p; ++q; v: v = v.insert(p,99);// leaves p pointing at the inserted element p: v: p: q:  Note: q is unaffected  Note: No elements moved around Stroustrup/Programming - Nov'13

erase() from list v: p = v.erase(p);// leaves p pointing at the element after the erased one p: v: p:  Note: list elements do not move when you insert() or erase() q: Stroustrup/Programming - Nov'13

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 ::size_type i = 0; i<v.size(); ++i)// longer but always correct … // do something with v[i] for(vector ::iterator p = v.begin(); p!=v.end(); ++p) …// do something with *p Less painful: for (auto = v.begin(); p!=v.end(); ++p) …// do something with *p 39Stroustrup/Programming - Nov'13

Ways of traversing a vector Know all three ways (iterator, subscript, auto) Know all three ways (iterator, subscript, auto) The subscript style is used in essentially every language The subscript style is used in essentially every language The iterator style is used in C (pointers only) and C++ The iterator style is used in C (pointers only) and C++ The iterator style is used for standard library algorithms The iterator style is used for standard library algorithms The subscript style doesn’t work for lists (in C++ and in most languages) The subscript style doesn’t work for lists (in C++ and in most languages) Use either way for vectors Use either way for vectors There are no fundamental advantages of one style over the other There are no fundamental advantages of one style over the other But the iterator style works for all sequences But the iterator style works for all sequences Prefer size_type over plain int Prefer size_type over plain int pedantic, but quiets compiler and prevents rare errors pedantic, but quiets compiler and prevents rare errors 40Stroustrup/Programming - Nov'13

Ways of traversing a vector for(vector ::iterator p = v.begin(); p!=v.end(); ++p) …// do something with *p for(vector ::value_type x : v) …// do something with x for(auto& x : v) …// do something with x “Range for” “Range for” Use for the simplest loops Use for the simplest loops Every element from begin() to end() Every element from begin() to end() Over one sequence Over one sequence When you don’t need to look at more than one element at a time When you don’t need to look at more than one element at a time When you don’t need to know the position of an element When you don’t need to know the position of an element 41Stroustrup/Programming - Nov'13

Vector vs. List By default, use a vector By default, use a vector You need a reason not to You need a reason not to You can “grow” a vector (e.g., using push_back()) You can “grow” a vector (e.g., using push_back()) You can insert() and erase() in a vector You can insert() and erase() in a vector Vector elements are compactly stored and contiguous Vector elements are compactly stored and contiguous For small vectors of small elements all operations are fast For small vectors of small elements all operations are fast compared to lists compared to lists If you don’t want elements to move, use a list If you don’t want elements to move, use a list You can “grow” a list (e.g., using push_back() and push_front()) You can “grow” a list (e.g., using push_back() and push_front()) You can insert() and erase() in a list You can insert() and erase() in a list List elements are separately allocated List elements are separately allocated Note that there are more containers, e.g., Note that there are more containers, e.g., map map unordered_map unordered_map Stroustrup/Programming - Nov'13 42

Some useful standard headers I/O streams, cout, cin, … I/O streams, cout, cin, … file streams file streams sort, copy, … sort, copy, … accumulate, inner_product, … accumulate, inner_product, … function objects function objects hash table hash table 43Stroustrup/Programming - Nov'13

Next lecture Map, set, and algorithms Map, set, and algorithms 44Stroustrup/Programming - Nov'13