Standard Template Library STL Chapter 4 General Summary
Standard Template Library (STL) Chapter 4, General Summary CS 244 Brent M. Dingle, Ph. D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Some content based on Book: Data Structures Using C++ 2 nd Ed. by D. S. Malik 2014
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++ 2 E 2
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 Our focus for today • Containers and iterators – Class templates Data Structures Using C++ 2 E 3
Container Types • STL containers categories – Sequence containers (sequential containers) – Associative containers Our focus for today i. e. std: : vector – Container adapters Data Structures Using C++ 2 E 4
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++ 2 E 5
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++ 2 E 6
Sequence Container: vector (cont’d. ) • Using a vector container in a program requires the following statement: – #include <vector> • Defining a vector container object – Specify object type – Example: vector<int> intlist; – Example: vector<string> string. List; Data Structures Using C++ 2 E 7
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 – EX 009_Heros. Inventory. cpp • We will alter this to use vectors • This is best done as …. – [next slide]
Class Activity – Heros. Invent 2 • On D 2 L download – EX 009_Heros. Inventory. cpp – and – EX 011_Heros. Invent 2. cpp • EX 009_Heros. Inventory is for comparison – Does not use std: : vector • We will be looking at Heros. Invent 2
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); Let’s take a look cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. Your shield is destroyed “ << "in a fierce battle. "; at the program inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); To use cout << "n. Your shield is destroyed “ << "in a fierce battle. "; std: : vectorinventory. pop_back(); need to include the file vector cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
Looking at Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); Make cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { << inventory[i] << endl; namedcout inventory } a variable of type: vector of strings cout << "You have " << inventory. size() << " items. n"; cout << "n. You were robbed of all of your “; cout << "n. Your items: n"; cout << "possessions by a thief. "; vector <string> for (unsigned int i = 0; i < inventory. size(); ++i) inventory; inventory. clear(); { cout << inventory[i] << endl; if (inventory. empty()) } { cout << "n. You have nothing. n"; cout << "n. You trade your sword for a battle axe. "; } inventory[0] = "battle axe"; else cout << "n. Your items: n"; { for (unsigned int i = 0; i < inventory. size(); ++i) cout << "n. You have at least one item. n"; { } cout << inventory[i] << endl; return 0; } }
Looking at Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; push_back is afor member vector (unsignedfunction int i = 0; iof < inventory. size(); ++i) { cout << inventory[i] << endl; It adds an element on to the end of the vector } cout << "You have " << inventory. size() << " items. n"; << "n. You were robbed of all of your “; So inventory[0]cout is “sword” cout << "n. Your items: n"; cout << "possessions by a thief. "; is “armor” for (unsigned int i = 0; i < inventory. size(); inventory[1] ++i) inventory. clear(); { inventory[2] is “shield” cout << inventory[i] << endl; if (inventory. empty()) } { brackets work just as they do for Note the square cout << "n. You have nothing. n"; arraysaxe. "; cout << "n. You trade your sword for a battle } inventory[0] = "battle axe"; else cout << "n. Your items: n"; { for (unsigned int i = 0; i < inventory. size(); ++i) cout << "n. You have at least one item. n"; { } cout << inventory[i] << endl; return 0; } }
Looking at Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "You have " << inventory. size() << " items. n"; cout << "n. You were robbed of all of your “; cout << "n. Your items: n"; cout << "possessions by a thief. "; size() is a member function of std: : vector for (unsigned int i = 0; i < inventory. size(); ++i) inventory. clear(); { cout << inventory[i] << endl; (inventory. empty()) it returns the number ofifelements currently } { stored in the vector object cout << "n. You have nothing. n"; cout << "n. You trade your sword for a battle axe. "; } inventory[0] = "battle axe"; recall: inventory is a vector object else cout << "n. Your items: n"; { for (unsigned int i = 0; i < inventory. size(); ++i) cout << "n. You have at least one item. n"; { } cout << inventory[i] << endl; return 0; } }
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. Your shield is destroyed “ << "in a fierce battle. "; Note the square brackets work just as they do for inventory. pop_back(); arrays cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) So we can use a{ typical for-loop to iterate cout << inventory[i] << endl; through the elements of a vector and print them } to the screen using cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
Looking at Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); if (inventory. empty()) { cout << "n. You have nothing. n"; cout << "n. You trade your sword for a battle axe. "; } inventory[0] = "battle axe"; else cout << "n. Your items: n"; { set the You int can use the square brackets to for (unsigned i = also 0; i < inventory. size(); ++i) cout << "n. You have at least one item. n"; value of elements in a vector (just as with arrays) { } cout << inventory[i] << endl; return 0; } }
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> << "n. Your shield is destroyed “ Because we have a vector ofcout string we can using namespace std; << "in a fierce battle. "; call the member functions of std: : string int main() inventory. pop_back(); { cout << "n. Your items: n"; vector<string> inventory; Note the use of the square brackets to specify for (unsigned int i = 0; the i < inventory. size(); ++i) inventory. push_back("sword"); element and the “dot” to call{the element’s member inventory. push_back("armor"); cout << inventory[i] << endl; function size() --- in this case std: string’s size() inventory. push_back("shield"); } function cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; inventory[0]. size() for (unsigned int i = 0; EX: i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() pop_back() is a member function of { std: : vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); It removes the last element from the inventory. push_back("shield"); cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } vector (in this case the shield) cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; causes the number of Which automatically for (unsigned int i = 0; i < inventory. size(); ++i) elements in the vector to be decreased { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() clear() is a member function of std: : vector { vector<string> inventory; inventory. push_back("sword"); It removes ALL the elements from the inventory. push_back("armor"); vector inventory. push_back("shield"); cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } and automatically adjusts the number of cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; elements in the vector object to be zero cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
Looking at Hero’s Inventory 2. 0 cout << "n. The item name '" << inventory[0] << "' has "; cout << inventory[0]. size() << " letters in it. n"; // Hero’s Inventory 2. 0 // Demonstrates vectors #include <iostream> #include <string> #include <vector> using namespace std; int main() empty() is a member function of std: : vector { vector<string> inventory; inventory. push_back("sword"); It returns true if there are no elements in the inventory. push_back("armor"); vector object inventory. push_back("shield"); cout << "n. Your shield is destroyed “ << "in a fierce battle. "; inventory. pop_back(); cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "You have " << inventory. size() << " items. n"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You trade your sword for a battle axe. "; inventory[0] = "battle axe"; cout << "n. Your items: n"; for (unsigned int i = 0; i < inventory. size(); ++i) { cout << inventory[i] << endl; } cout << "n. You were robbed of all of your “; cout << "possessions by a thief. "; inventory. clear(); } if (inventory. empty()) { cout << "n. You have nothing. n"; } else { cout << "n. You have at least one item. n"; } return 0;
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
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 ++cnt. Itr; *cnt. Itr; Data Structures Using C++ 2 E 23
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++ 2 E 24
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<int>: : iterator int. Vec. Iter; Data Structures Using C++ 2 E 25
Declaring an Iterator to a Vector Container • Requirements for using typedef iterator – Container name (vector) – Container element type – Scope resolution operator • ++int. Vec. Iter – Advances iterator int. Vec. Iter to next element into the container • *int. Vec. Iter – Returns element at current iterator position Data Structures Using C++ 2 E 26
Declaring an Iterator to a Vector Container • Using an iterator into a vector container – Manipulating element type to be int Data Structures Using C++ 2 E 27
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++ 2 E 28
Containers and the Functions begin and end (cont’d. ) TABLE 4 -4 Functions to determine the size of a vector container Data Structures Using C++ 2 E 29
Marker Slide • Any Questions on – Concept of Iterators • Next Up – EXAMPLE to demonstrate: • Iterators – with vector’s begin(), end(), dereferencing an iterator
Class Activity – Heros. Invent 3 • On D 2 L download – – – EX 009_Heros. Inventory. cpp and EX 011_Heros. Invent 2. cpp and EX 012_Heros. Invent 3. cpp • We will now look at Heros. Invent 3
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> Let’s take using namespace std; program int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; a look at the cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> Same using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; includes cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); Same initialization cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; The line: cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() my. Iterator << " letters in it. n"; vector<string>: : iterator cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; Declares an iterator named my. Iterator for a vector that cout << "n. You recover a crossbow from a slain enemy. "; contains string objects. inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; It will be usedfor to(iter identify a particular in a ++iter) = inventory. begin(); iterelement != inventory. end(); { container } cout << *iter << endl; You may think of an iterator as a post-it note that is cout << "Your items: n"; cout << "n. Your armor is destroyed in a fierce battle. "; for (iter = inventory. begin(); iter != inventory. end(); ++iter) “stuck” to an element in a container + 2)); inventory. erase((inventory. begin() { So the iterator itself is NOT an element cout << *iter << endl; cout << "n. Your items: n"; } it REFERS (points) an element for (iter = inventory. begin(); iter !=to inventory. end(); ++iter) { cout << "n. You trade your sword for a battle axe. "; cout << *iter << endl; my. Iterator = inventory. begin(); Now we can stick the my. Iterator post-it note on a } *my. Iterator = "battle axe"; return specific element in 0; inventory. cout << "n. Your items: n"; } for (iter = inventory. begin(); iter != inventory. end(); Once ++iter) we’ve done that we can access the element { even change its value if we want cout << *iter << endl; } and
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { This is a constant iterator cout << *iter << endl; for a vector that contains strings } cout << "Your items: n"; cout << "n. Your armor is destroyed in a fierce battle. "; for (iter = inventory. begin(); iter != inventory. end(); ++iter)in this Constant case means you inventory. erase((inventory. begin() + 2)); { cannot use it to change the value of cout << *iter << endl; cout << "n. Your items: n"; } the element to it refers iter != inventory. end(); ++iter) for which (iter = inventory. begin(); cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } { } cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { This is a constant iterator cout << *iter << endl; for a vector that contains strings } cout << "Your items: n"; cout << "n. Your armor is destroyed in a fierce battle. "; for (iter = inventory. begin(); iter != inventory. end(); Which++iter) meansinventory. erase((inventory. begin() you cannot use it to + 2)); { change the value of the element to cout << *iter << endl; cout << "n. Your items: n"; } which it refers for (iter = inventory. begin(); iter != inventory. end(); ++iter) cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); Use these *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } { cout << *iter << endl; to prevent accidental changes } } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; Looping through the content of the vector cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; We use the constant iter << "n. Youwe recover crossbow a slain enemy. "; – cout because do anot wantfrom to change any inventory. insert(inventory. begin(), "crossbow"); values, just print them out cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; Looping through the content of the vector cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; We use the constant iter << "n. Youwe recover crossbow a slain enemy. "; – cout because do anot wantfrom to change any inventory. insert(inventory. begin(), "crossbow"); values, just print them out cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) begin() is a member function of { cout << *iter << endl; std: : vector } It returns an iterator that references coutfirst << "n. Your armor isin destroyed in a fierce battle. "; the element the vector inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; Looping through the content of the vector cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; We use the constant iter << "n. Youwe recover crossbow a slain enemy. "; – cout because do anot wantfrom to change any inventory. insert(inventory. begin(), "crossbow"); values, just print them out cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) begin() is a member function of { cout << *iter << endl; std: : vector } It returns an iterator that references coutfirst << "n. Your armor isin destroyed in a fierce battle. "; the element the vector inventory. erase((inventory. begin() + 2)); cout << is "n. Your items: n"; function of std: : vector end() a member (iter = inventory. begin(); iter != inventory. end(); ++iter) It for returns an iterator that references { << *iter << endl; thecout one past last element in the vector } return 0; } ++iter moves the “post it note” to the next element in the vector
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; Looping through the content of the vector cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; We use the constant iter << "n. Youwe recover crossbow a slain enemy. "; – cout because do anot wantfrom to change any inventory. insert(inventory. begin(), "crossbow"); values, just print them out cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) begin() is a member function of { cout << *iter << endl; std: : vector } It returns an iterator that references coutfirst << "n. Your armor isin destroyed in a fierce battle. "; the element the vector inventory. erase((inventory. begin() + 2)); cout << is "n. Your items: n"; function of std: : vector end() a member (iter = inventory. begin(); iter != inventory. end(); ++iter) It for returns an iterator that references { << *iter << endl; thecout one past last element in the vector } return 0; } ++iter moves the “post it note” to the next element in the vector
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << << "n. The ’" << *my. Iterator << "’ has "; cout *iteritem << name endl; cout << my. Iterator->size() << " letters in it. n"; cout prints << "n. Youout recover crossbowof from enemy. "; This thea value thea slain element inventory. insert(inventory. begin(), "crossbow"); referenced by the iterator cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) The asterisk (*), or “star, ” { cout dereference << *iter << endl; operator in C++ is the } cout << "n. Your armor is destroyed in a fierce battle. "; Recall the iterator is referencing an inventory. erase((inventory. begin() + 2)); element cout << "n. Your items: n"; So we naturally must for (iter = inventory. begin(); iter != inventory. end(); ++iter) dereference it to get the element value { cout << *iter << endl; } i. e. we want to use thing the sticky return 0; } note is attached too, not the sticky note itself
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); Set my. Iterator to reference the first element in the vector cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { Use the NON-constant iterator named cout << *iter << endl; } my. Iterator return 0; because we are going to change the } value of the thing it references
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); Change the value of the element my. Iterator references to be “battle axe” cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { Socout in this case the first element in << *iter << endl; } inventory will be changed to “battle axe” return 0; } 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 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); Same stuff as before cout << "n. Your items: n"; for (iter =out inventory. begin(); iter != ++iter) Printing the contents ofinventory. end(); the inventory { cout << *iter << endl; } return 0; }
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; Here we are using *my. Iterator cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); just as we used inventory[0] in the cout << "n. Your items: n"; previous version for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; To access the member functions of the inventory. insert(inventory. begin(), "crossbow"); element referenced by my. Iterator we can << "n. Your items: n"; dereference cout it and use the “dot” to call for (iter = inventory. begin(); iter != inventory. end(); ++iter) the member {function, as here: } cout << *iter << endl; (*my. Iterator). size cout << "Your items: n"; cout << "n. Your armor is destroyed in a fierce battle. "; for (iter = inventory. begin(); iter != inventory. end(); ++iter) inventory. erase((inventory. begin() + 2)); This does require the parentheses, and { cout << *iter << endl; cout << "n. Your items: n"; is a little awkward to type } for (iter = inventory. begin(); iter != inventory. end(); ++iter) cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } { } cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> cout << "n. The item name ’" << *my. Iterator << "’ has "; #include <string> cout << (*my. Iterator). size() << " letters in it. n"; #include <vector> cout << "n. The item name ’" << *my. Iterator << "’ has "; using namespace std; cout << my. Iterator->size() << " letters in it. n"; int main() { cout << "n. You recover a crossbow from a slain enemy. "; vector<string> inventory; inventory. insert(inventory. begin(), "crossbow"); There is a slightly better and prettier way inventory. push_back("sword"); inventory. push_back("armor"); coutis << "n. Your to do the same thing and that to useitems: n"; the inventory. push_back("shield"); for (iter = inventory. begin(); iter != inventory. end(); ++iter) arrow operator, -> { vector<string>: : iterator my. Iterator; cout << *iter << endl; (a minus sign followed by a greater than vector<string>: : const_iterator iter; } sign) cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) my. Iterator->size() { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); does the same thing as cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); (*my. Iterator). size() *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include insert()<vector> is a member function using namespace std; int main() {It takes 2 parameters vector<string> inventory; - an iterator (reference to inventory. push_back("sword"); inventory. push_back("armor"); - a new element to insert inventory. push_back("shield"); of std: : vector an element) vector<string>: : iterator my. Iterator; The new element is inserted before the vector<string>: : const_iterator iter; element referenced by the iterator cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) inventory. insert(inventory. begin(), "crossbow"); { cout << *iter << endl; } inserts “crossbow” before the first element already in the vector cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; So “crossbow” becomes the first element in } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) the vector { cout << *iter << endl; } cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; cout << "Your items: n"; cout << "n. Your armor is destroyed in a fierce battle. "; for (iter = inventory. begin(); iter != inventory. end(); ++iter) inventory. erase((inventory. begin() + 2)); Same stuff as before { cout << *iter << endl; cout << "n. Your items: n"; } for (iter = inventory. begin(); iter != inventory. end(); ++iter) Printing out the contents of the inventory cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } { } cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> erase() is a member function of #include <vector> std: : vector using namespace std; int main() { It removesinventory; the element referenced by the vector<string> inventory. push_back("sword"); iterator sent from the vector inventory. push_back("armor"); AND adjusts all the remaining elements inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } in the vector accordingly vector<string>: : iterator my. Iterator; vector<string>: : const_iterator iter; Caution: This will invalidate any iterators cout << "Your items: n"; set(iter previously that reference elements for = inventory. begin(); iter != inventory. end(); ++iter) { after the erased one cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle This deletes theaxe"; 3 rd element from the cout << "n. Your items: n"; inventory vector object for (iter = inventory. begin(); iter != inventory. end(); ++iter) {In this case it removes “armor” from the cout << *iter << endl; }vector } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Hero’s Inventory 3. 0 // Demonstrates iterators #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> inventory; inventory. push_back("sword"); inventory. push_back("armor"); inventory. push_back("shield"); cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << (*my. Iterator). size() << " letters in it. n"; cout << "n. The item name ’" << *my. Iterator << "’ has "; cout << my. Iterator->size() << " letters in it. n"; cout << "n. You recover a crossbow from a slain enemy. "; inventory. insert(inventory. begin(), "crossbow"); cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } vector<string>: : iterator my. Iterator; vector<string>: : const_iterator Same stuff as before iter; cout << "Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) Printing out the contents of the inventory { cout << *iter << endl; } cout << "n. You trade your sword for a battle axe. "; my. Iterator = inventory. begin(); *my. Iterator = "battle axe"; cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } cout << "n. Your armor is destroyed in a fierce battle. "; inventory. erase((inventory. begin() + 2)); } cout << "n. Your items: n"; for (iter = inventory. begin(); iter != inventory. end(); ++iter) { cout << *iter << endl; } return 0;
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 – High. Scores • Download, compile, look over… – EX 013_High. Scores. cpp • Go to next slide
Looking at Example: High Scores // Demonstrates algorithms #include <iostream> To use STL algorithms #include <vector> #include <algorithm> algorithm #include <ctime> needs to be included #include <cstdlib> using namespace std; int main() { vector<int>: : const_iterator iter; cout << "Creating a list of scores. "; vector<int> scores; iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { the file cout << "Score found. n"; } else { cout << "Score not found. n"; } cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); scores. push_back(1000); scores. push_back(3800); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> using namespace std; int main() { vector<int>: : const_iterator iter; We will begin with 3 cout << "Creating a list of scores. "; coded, high scores vector<int> scores; cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); arbitrary, hardrandom_shuffle(scores. begin(), scores. end()); scores. push_back(1000); scores. push_back(3800); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> using namespace std; int main() { vector<int>: : const_iterator iter; cout << "Creating a list of scores. "; vector<int> scores; cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); scores. push_back(1000); scores. push_back(3800); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { << *iter <<by endl; We will test the findcout algorithm having cout << "n. High Scores: n"; } the user enter a score to find cout << "n. Sorting scores. "; for (iter = scores. begin(); iter != scores. end(); ++iter) sort(scores. begin(), scores. end()); { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores // Demonstrates algorithms #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> using namespace std; int main() { vector<int>: : const_iterator iter; cout << "Creating a list of scores. "; vector<int> scores; iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; std: : find is a “global” function } else reference iterator It takes a starting and an ending{ reference iterator cout << "Score not found. n"; } Recall scores. end() is a reference cout << "n. Randomizing scores. "; one past the last element in the scores srand(static_cast<unsigned int>(time(0))); vector (i. e. an invalid element indicating scores. end()); the end of the random_shuffle(scores. begin(), vector) scores. push_back(1000); scores. push_back(3800); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> std: : find() returns the reference to the #include <algorithm> #include <ctime> element that has the value of sent to find #include <cstdlib> using namespace std; If no such element was found it returns it int main() returns a reference to scores. end() { vector<int>: : const_iterator iter; (i. e. an invalid element) cout << "Creating a list of scores. "; vector<int> scores; cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); scores. push_back(1000); scores. push_back(3800); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> As we will be wanting a different random #include <algorithm> #include <ctime> shuffle each time we run the program #include <cstdlib> - we need to seed the random number using namespace std; generator int main() - prior to calling the { vector<int>: : const_iterator iter; algorithm std: : random_shuffle() cout << "Creating a list of scores. "; function vector<int> scores; cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); scores. push_back(1000); scores. push_back(3800); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> std: : random_shuffle() is a global function #include <algorithm> #include <ctime> defined in the algorithm file #include <cstdlib> using namespace std; It requires you to send a starting iterator int main() and an ending iterator of a container for { vector<int>: : const_iterator the element you wantiter; shuffled cout << "Creating a list of scores. "; vector<int> scores; cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); We want to shuffle all the elements in the scores. push_back(1000); scores array so we send scores. begin() scores. push_back(3800); and scores. end() cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> using namespace std; int main() { vector<int>: : const_iterator iter; cout << "Creating a list of scores. "; vector<int> scores; Display the scores. push_back(1000); scores. push_back(3800); randomized scores. push_back(9400); cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); to show they are cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Looking at Example: High Scores iter = find(scores. begin(), scores. end(), score); if (iter != scores. end()) { cout << "Score found. n"; } else { cout << "Score not found. n"; } // High Scores // Demonstrates algorithms #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> std: : sort is a global function declared in using namespace std; the algorithm file int main() { vector<int>: : const_iterator Like random_shuffle iter; you send it a cout << "Creating a list of scores. "; starting iterator and an ending iterator for vector<int> scores; cout << "n. Randomizing scores. "; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores. begin(), scores. end()); the elements you want sorted scores. push_back(1000); scores. push_back(3800); It will sort them in ascending scores. push_back(9400); cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Sorting scores. "; sort(scores. begin(), scores. end()); order from least to greatest cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } cout << "n. Finding a score. "; int score; cout << "n. Enter a score to find: "; cin >> score; } cout << "n. High Scores: n"; for (iter = scores. begin(); iter != scores. end(); ++iter) { cout << *iter << endl; } return 0;
Marker Slide • Any Questions on – High Scores Program • STL Algorithm Use • Next Up – Summary – Free Play
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++ 2 E 67
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 Data Structures Using C++ 2 E 68
Other STL Containers Container Description std: : deque Double ended queue std: : list Linear list std: : map Collection of key/value pairs in which each key is associated with exactly one value std: : multimap Collection of key/value pairs in which each key may be associated with more than one value std: : priority_queue As the name says: prioritized line std: : queue As the name says: a line std: : set Collection in which each element is unique std: : stack As the name says: a stack of elements on top of another std: : vector Dynamic 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?
- Slides: 71