Standard Template Library (STL) Chapter 4, General Summary Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics,

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Data Structures Using C++ 2E
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Beginning C++ Through Game Programming, Second Edition
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Computer Science 1620 Loops.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
C++ for Engineers and Scientists Third Edition
Data Structures Using C++ 2E
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.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Templates and the STL.
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
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
File I/O ifstreams and ofstreams Sections 11.1 &
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Review and Prepare for Test 1 Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Lecture 7 : Intro. to STL (Standard Template Library)
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
References and Pointers CS 244 Connect Speakers for this Presentation Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
The Standard Template Library Container Classes Version 1.0.
Standard Template Library (STL) - Use Vector and Deque
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Using Sequential Containers Lecture 8 Hartmut Kaiser
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
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.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
CS212: Object Oriented Analysis and Design
Standard Template Library
Standard Template Library (STL)
Loops, Arrays, and STL: Objects, Strings, Vectors
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
More on Merge Sort CS 244 This presentation is not given in class
Chapter 22: Standard Template Library (STL)
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
Collections Intro What is the STL? Templates, collections, & iterators
Presentation transcript:

Standard Template Library (STL) Chapter 4, General Summary Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout 2014 Some content based on Book: Data Structures Using C++ 2 nd Ed. by D.S. Malik CS 244

Data Structures Using C++ 2E2 Objectives Learn about the Standard Template Library (STL) Become familiar with the three basic components of the STL: containers, iterators, and algorithms Explore how vector (and deque) containers are used to manipulate data in a program Discover the use of iterators

Data Structures Using C++ 2E3 Components of the STL Most program’s main objective is to manipulate data and generate results –Requires ability to store data, access data, and manipulate data STL components –Containers –Iterators: step through container elements –Algorithms: manipulate data Containers and iterators –Class templates Our focus for today

Data Structures Using C++ 2E4 Container Types STL containers categories –Sequence containers (sequential containers) –Associative containers –Container adapters Our focus for today i.e. std::vector

Data Structures Using C++ 2E5 Sequence Containers Every object has a specific position Predefined sequence containers –vector, deque, list Sequence container vector –Logically: same as arrays –Processed like arrays Side Point: All containers –Use same names for common operations –Have specific operations

Data Structures Using C++ 2E6 Sequence Container: vector Vector container –Stores, manages objects in a dynamic array –Elements accessed randomly –Time-consuming item insertion: middle, beginning –Fast item insertion: end Class implementing vector container –vector Header file containing the class vector –vector

Data Structures Using C++ 2E7 Sequence Container: vector (cont’d.) Using a vector container in a program requires the following statement: –#include Defining a vector container object –Specify object type –Example: vector intlist; –Example: vector stringList;

Marker Slide Any Questions on –Concept of std::vector Next Up –EXAMPLE to demonstrate: STL vectors are std:: vector –push_back(), size(), indexing, pop_back(), clear(), empty() –and then another example for Iterators –with vector’s begin(), end(), dereferencing an iterator

Hero’s Inventory 2.0 Examine: Hero’s Inventory 1.0 –EX009_HerosInventory.cpp We will alter this to use vectors This is best done as …. –[next slide]

Class Activity – HerosInvent2 On D2L download –EX009_HerosInventory.cpp –and –EX011_HerosInvent2.cpp EX009_HerosInventory is for comparison –Does not use std::vector We will be looking at HerosInvent2

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } Let’s take a look at the program

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } To use std::vector need to include the file vector

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } Make a variable named inventory of type: vector of strings vector inventory;

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } push_back is a member function of vector It adds an element on to the end of the vector So inventory[0] is “sword” inventory[1] is “armor” inventory[2] is “shield” Note the square brackets work just as they do for arrays

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } size() is a member function of std::vector it returns the number of elements currently stored in the vector object recall: inventory is a vector object

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } Note the square brackets work just as they do for arrays So we can use a typical for-loop to iterate through the elements of a vector and print them to the screen using cout

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } You can also use the square brackets to set the value of elements in a vector (just as with arrays)

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } Because we have a vector of string we can call the member functions of std::string Note the use of the square brackets to specify the element and the “dot” to call the element’s member function size() --- in this case std:string’s size() function EX: inventory[0].size()

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } pop_back() is a member function of std::vector It removes the last element from the vector (in this case the shield) Which automatically causes the number of elements in the vector to be decreased

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } clear() is a member function of std::vector It removes ALL the elements from the vector and automatically adjusts the number of elements in the vector object to be zero

Looking at Hero’s Inventory 2.0 // Hero’s Inventory 2.0 // Demonstrates vectors #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); cout << "You have " << inventory.size() << " items.\n"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nThe item name \'" << inventory[0] << "\' has "; cout << inventory[0].size() << " letters in it.\n"; cout << "\nYour shield is destroyed “ << "in a fierce battle."; inventory.pop_back(); cout << "\nYour items:\n"; for (unsigned int i = 0; i < inventory.size(); ++i) { cout << inventory[i] << endl; } cout << "\nYou were robbed of all of your “; cout << "possessions by a thief."; inventory.clear(); if (inventory.empty()) { cout << "\nYou have nothing.\n"; } else { cout << "\nYou have at least one item.\n"; } return 0; } empty() is a member function of std::vector It returns true if there are no elements in the vector object

Marker Slide Any Questions on –Concept of Hero’s Inventory 2 STL vectors are std:: vector –push_back(), size(), indexing, pop_back(), clear(), empty() Next Up –Iterators

Data Structures Using C++ 2E23 Iterators Work like pointers Point to elements of a container (sequence or associative) Allow successive access to each container element Two most common operations on iterators ++ (increment operator) * (dereferencing operator) Examples ++cntItr; *cntItr;

Data Structures Using C++ 2E24 Types of Iterators Input iterators Output iterators Forward iterators Bidirectional iterators Random access iterators Aside: The leading adjective pretty much defines the type Input Iterators -> used for input…

Data Structures Using C++ 2E25 Declaring an Iterator to a Vector Container class vector contains typedef iterator –Declared as a public member –Vector container iterator Declared using typedef iterator –Example vector ::iterator intVecIter;

Data Structures Using C++ 2E26 Declaring an Iterator to a Vector Container Requirements for using typedef iterator –Container name (vector) –Container element type –Scope resolution operator ++intVecIter –Advances iterator intVecIter to next element into the container *intVecIter –Returns element at current iterator position

Data Structures Using C++ 2E27 Declaring an Iterator to a Vector Container Using an iterator into a vector container –Manipulating element type to be int

Data Structures Using C++ 2E28 Containers and the Functions begin and end begin –Returns position of the first element into the container end –Returns position of the last element into the container Neither function has any parameters W.R.T. the class vector –It contains member functions used to find number of elements currently in the container

Data Structures Using C++ 2E29 Containers and the Functions begin and end (cont’d.) TABLE 4-4 Functions to determine the size of a vector container

Marker Slide Any Questions on –Concept of Iterators Next Up –EXAMPLE to demonstrate: Iterators –with vector’s begin(), end(), dereferencing an iterator

Class Activity – HerosInvent3 On D2L download –EX009_HerosInventory.cpp –and –EX011_HerosInvent2.cpp –and –EX012_HerosInvent3.cpp We will now look at HerosInvent3

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Let’s take a look at the program

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Same includes

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Same initialization

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } The line: vector ::iterator myIterator Declares an iterator named myIterator for a vector that contains string objects. It will be used to identify a particular element in a container You may think of an iterator as a post-it note that is “stuck” to an element in a container So the iterator itself is NOT an element it REFERS (points) to an element Now we can stick the myIterator post-it note on a specific element in inventory. Once we’ve done that we can access the element and even change its value if we want

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } This is a constant iterator for a vector that contains strings Constant in this case means you cannot use it to change the value of the element to which it refers

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } This is a constant iterator for a vector that contains strings Which means you cannot use it to change the value of the element to which it refers Use these to prevent accidental changes

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Looping through the content of the vector We use the constant iter – because we do not want to change any values, just print them out

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Looping through the content of the vector We use the constant iter – because we do not want to change any values, just print them out begin() is a member function of std::vector It returns an iterator that references the first element in the vector

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Looping through the content of the vector We use the constant iter – because we do not want to change any values, just print them out begin() is a member function of std::vector It returns an iterator that references the first element in the vector end() is a member function of std::vector It returns an iterator that references the one past last element in the vector ++iter moves the “post it note” to the next element in the vector

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Looping through the content of the vector We use the constant iter – because we do not want to change any values, just print them out begin() is a member function of std::vector It returns an iterator that references the first element in the vector end() is a member function of std::vector It returns an iterator that references the one past last element in the vector ++iter moves the “post it note” to the next element in the vector

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } cout << *iter << endl; This prints out the value of the element referenced by the iterator The asterisk (*), or “star,” is the dereference operator in C++ Recall the iterator is referencing an element So we naturally must dereference it to get the element value i.e. we want to use the thing the sticky note is attached too, not the sticky note itself

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Set myIterator to reference the first element in the vector Use the NON-constant iterator named myIterator because we are going to change the value of the thing it references

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Change the value of the element myIterator references to be “battle axe” So in this case the first element in inventory will be changed to “battle axe” Note: again we must use the * to dereference the iterator to change the value of the element it references

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Same stuff as before Printing out the contents of the inventory

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Here we are using *myIterator just as we used inventory[0] in the previous version

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } To access the member functions of the element referenced by myIterator we can dereference it and use the “dot” to call the member function, as here: (*myIterator).size This does require the parentheses, and is a little awkward to type

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } There is a slightly better and prettier way to do the same thing and that is to use the arrow operator, -> (a minus sign followed by a greater than sign) myIterator->size() does the same thing as (*myIterator).size()

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } insert() is a member function of std::vector It takes 2 parameters - an iterator (reference to an element) - a new element to insert The new element is inserted before the element referenced by the iterator inventory.insert(inventory.begin(), "crossbow"); inserts “crossbow” before the first element already in the vector So “crossbow” becomes the first element in the vector

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Same stuff as before Printing out the contents of the inventory

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } erase() is a member function of std::vector It removes the element referenced by the iterator sent from the vector AND adjusts all the remaining elements in the vector accordingly Caution: This will invalidate any iterators set previously that reference elements after the erased one inventory.erase((inventory.begin() + 2)); This deletes the 3 rd element from the inventory vector object In this case it removes “armor” from the vector

Looking at Hero’s Inventory 3.0 // Hero’s Inventory 3.0 // Demonstrates iterators #include using namespace std; int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); vector ::iterator myIterator; vector ::const_iterator iter; cout << "Your items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYou trade your sword for a battle axe."; myIterator = inventory.begin(); *myIterator = "battle axe"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nThe item name ’" << *myIterator << "’ has "; cout << (*myIterator).size() << " letters in it.\n"; cout << "\nThe item name ’" << *myIterator << "’ has "; cout size() << " letters in it.\n"; cout << "\nYou recover a crossbow from a slain enemy."; inventory.insert(inventory.begin(), "crossbow"); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } cout << "\nYour armor is destroyed in a fierce battle."; inventory.erase((inventory.begin() + 2)); cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); ++iter) { cout << *iter << endl; } return 0; } Same stuff as before Printing out the contents of the inventory

Marker Slide Any Questions on –Concept of Iterators –Hero’s Inventory 3 Next Up –STL Algorithms EXAMPLE to demonstrate: –STL Algorithm Use

STL Algorithms STL defines a group of algorithms that allow the manipulation of elements in containers by using iterators For the majority of this class you will be implementing your own versions of various algorithms –BUT it is good to be familiar with those included in STL and how to use them

High Scores Program To demonstrate the use of STL algorithms we will look through a program that maintains a list of player high scores The program will create a vector of high score Using STL algorithms it will –search, –shuffle, –and sort the scores

Class Activity – HighScores Download, compile, look over… –EX013_HighScores.cpp Go to next slide

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } To use STL algorithms the file algorithm needs to be included

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } We will begin with 3 arbitrary, hard- coded, high scores

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } We will test the find algorithm by having the user enter a score to find

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } std::find is a “global” function It takes a starting reference iterator and an ending reference iterator Recall scores.end() is a reference one past the last element in the scores vector (i.e. an invalid element indicating the end of the vector)

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } std::find() returns the reference to the element that has the value of sent to find If no such element was found it returns it returns a reference to scores.end() (i.e. an invalid element)

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } As we will be wanting a different random shuffle each time we run the program -we need to seed the random number generator -prior to calling the std::random_shuffle() algorithm function

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } std::random_shuffle() is a global function defined in the algorithm file It requires you to send a starting iterator and an ending iterator of a container for the element you want shuffled We want to shuffle all the elements in the scores array so we send scores.begin() and scores.end()

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } Display the scores to show they are randomized

Looking at Example: High Scores // High Scores // Demonstrates algorithms #include using namespace std; int main() { vector ::const_iterator iter; cout << "Creating a list of scores."; vector scores; scores.push_back(1000); scores.push_back(3800); scores.push_back(9400); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast (time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } return 0; } std::sort is a global function declared in the algorithm file Like random_shuffle you send it a starting iterator and an ending iterator for the elements you want sorted It will sort them in ascending order from least to greatest

Marker Slide Any Questions on –High Scores Program STL Algorithm Use Next Up –Summary –Free Play

Data Structures Using C++ 2E67 Summary STL –Provides class templates Process lists, stacks, and queues –Three main components Containers, iterators, and algorithms –STL containers: class templates Iterators –Step through the elements of a container Algorithms –Manipulate elements in a container

Data Structures Using C++ 2E68 Summary (cont’d.) Main categories of containers –Sequence containers, associative containers, container adapters Three predefined sequence containers –vector, deque, and list Five categories of iterators: –input, output, forward, bidirectional, random access iterator Not talked about (see book) –copy algorithm Copies elements in a given range to another place –Function copy, using an ostream iterator Can output the elements of a container

Other STL Containers ContainerDescription std::dequeDouble ended queue std::listLinear list std::mapCollection of key/value pairs in which each key is associated with exactly one value std::multimapCollection of key/value pairs in which each key may be associated with more than one value std::priority_queueAs the name says: prioritized line std::queueAs the name says: a line std::setCollection in which each element is unique std::stackAs the name says: a stack of elements on top of another std::vectorDynamic array Important to understand: These are specific implementations of abstract data types.

Free Play – Things to Work On Homework 3, 4, and/or 5 Look over the various examples discussed

The End Or is it?