Beginning C++ Through Game Programming, Second Edition

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
DATA STRUCTURES USING C++ Chapter 5
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Data Structures: A Pseudocode Approach with C
 2006 Pearson Education, Inc. All rights reserved Templates.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
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
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
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.
Templates and the STL.
Object Oriented Data Structures
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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
Chapter 16: Searching, Sorting, and the vector Type.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Generic Programming Using the C++ Standard Template Library.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library (STL) Chapter 4, General Summary Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics,
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 7 : Intro. to STL (Standard Template Library)
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
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.
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.
Standard Template Library
CS212: Object Oriented Analysis and Design
Standard Template Library
C++ Standard Library.
Chapter 8 Arrays, Strings and pointers
Standard Template Library (STL)
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 9 One-Dimensional Arrays
Standard Template Library Model
Standard Template Library
the Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
The List Container and Iterators
Presentation transcript:

Beginning C++ Through Game Programming, Second Edition by Michael Dawson

The Standard Template Library: Hangman Chapter 4 The Standard Template Library: Hangman

Objectives Use vectors to work with sequences Use vector member functions to manipulate sequence elements Use iterators to move through sequences Use library algorithms to work with groups of elements Plan your programs with pseudocode

The Standard Template Library (STL) Powerful collection of code at your disposal Facilities for storing, manipulating and retrieving data Provides containers, algorithms, and iterators

Vectors One kind of container provided by the STL A dynamic array—can grow and shrink in size as needed Member functions to manipulate elements All of the functionality of an array plus more

Preparing to Use Vectors All STL components live in the std namespace Have to include the file that contains definition: #include <vector>

Declaring a Vector Declare vector to hold string objects vector<string> inventory; Declare vector to hold string objects, starting size 10 vector<string> inventory(10); Declared vector with size 10, all elements initialized to "nothing" vector<string> inventory(10, "nothing"); Declare vector and initialize it with the contents of another vector, myStuff vector<string> inventory(myStuff);

Useful Vector Member Functions push_back()—Adds a new element to the end of a vector size()—Returns the size of a vector pop_back()—Removes the last element of a vector and reduces the vector size by one clear()—Removes all of the items of a vector and sets its size to 0 empty()—Returns true if the vector is empty; otherwise, it returns false. insert()—Inserts a new element into a vector erase()—Removes an element from a vector

Indexing Vectors Just as with arrays, can index vectors by using the subscripting operator Display all elements: for (int i = 0; i < inven.size(); ++i) cout << inven[i] << endl; Replace the hero’s first item: inven[0] = "battle axe"; Can’t increase a vector’s size with subscripting operator; to add a new element at the end of a vector, use push_back().

Iterators Identify particular element in a sequence Can access or change the value of an element Key to using containers to their fullest Use them to move through a sequence container Some parts of the STL require iterators

Declaring Iterators Generic declaration: container-type<object-type>::iterator iterVar; Declare an iterator named myIterator for a vector that can contain string objects: vector<string>::iterator myIterator; Declare constant iterator ("read-only" access): vector<string>::const_iterator iter;

begin() Vector Member Function begin()—Returns an iterator that refers to a container’s first element

end() Vector Member Function end()—Returns an iterator one past the last element in a container

Iterating Through a Vector for (iter = inventory.begin(); iter != inventory.end(); ++iter) cout << *iter << endl; begin()—Returns an iterator that refers to first element of inventory end()—Returns an iterator one past the last element of inventory ++iter—Increments iter, which moves it to the next element in inventory *iter—Dereferences iter, returns the value it refers to

Accessing Values Through an Iterator *myIterator = "battle axe"; Alter values to which myIterator refers myIterator does not change Calls the size() method of the element myIterator refers to cout << (*myIterator).size(); Same functionality ("syntactic sugar") cout << myIterator->size();

Algorithms Manipulate elements in containers through iterators Searching, sorting, copying and more Generic—The same algorithm can work with elements of different container types To use, include the file with their definitions: #include <algorithm> Algorithms (like all STL components) live in the std namespace Can work with some containers defined outside of the STL (like string objects)

Useful Algorithms find()—Searches elements for a value random_shuffle()—Randomizes order of elements sort()—Sorts elements (in ascending order, by default)

Vector Performance Vectors (and other STL containers) are incredibly efficient But containers have their strengths and weaknesses Pick the right container for the job

Vector Growth When vectors grow beyond current size, vector might be copied to new area of memory However, reallocation might not occur at a performance-critical part of your program With small vectors, the reallocation cost might be insignificant

Vector Capacity Capacity not the same as size; it's how many elements a vector can hold until reallocation. capacity()—Returns the number of elements that a vector can hold before a reallocation reserve()—Increases the capacity of a vector Don’t obsess over performance

Vector Strengths and Weaknesses push_back() and pop_back() member functions are extremely efficient But insert() and erase() can require more work Another STL container, list, allows for efficient insertion and deletion, regardless of the sequence size Just because you want to insert or delete elements from the middle of a sequence doesn’t mean you should abandon the vector

Other STL Containers

Pseudocode Planning non-trivial programs can save much time and heartache Pseudocode is a language that falls somewhere between English and a formal programming language; useful for sketching out programs. Pseudocode example: If you can think of a new and useful product Then that’s your product Otherwise Repackage an existing product as your product Make an infomercial about your product Show the infomercial on TV Charge $100 per unit of your product Sell 10,000 units of your product

Stepwise Refinement Taking complex steps in pseudocode and breaking them down into a series of simpler steps Plan becomes closer to programming code Make an infomercial becomes: Write a script for an infomercial about your product Rent a TV studio for a day Hire a production crew Hire an enthusiastic audience Film the infomercial

Summary The Standard Template Library (STL) is a powerful collection of programming code that provides containers, algorithms, and iterators Containers are objects that let you store and access collections of values of the same type Algorithms can be used with containers and provide common functions for working with groups of objects Iterators are objects that identify elements in containers and can be manipulated to move among elements To get the value referenced by an iterator, you can dereference the iterator using the dereference operator (*)

Summary (cont.) A vector is one kind of sequential container provided by the STL (like dynamic array) Very efficient to iterate through a vector Very efficient to insert or remove an element from the end of a vector It can be inefficient to insert or delete elements from the middle of a vector, especially if the vector is large Pseudocode, which falls somewhere between English and a programming language, is used to plan programs Stepwise refinement is a process used to rewrite pseudocode to make it ready for implementation