THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Similar documents
Lecture on pointers, references, and arrays and vectors

Standard Template Library

Containers in C++ and Java

CS197c: Programming in C++

G52CPP C++ Programming Lecture 18

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Function Templates. Consider the following function:

STL Standard Template Library

Template based set of collection classes STL collection types (container types)

The Standard Template Library. EECS 211 Winter 2018

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers

SSE2034: System Software Experiment 3

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

1. The term STL stands for?

Computational Physics

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Dynamic Data Structures

CSI33 Data Structures

DELHI PUBLIC SCHOOL TAPI

Exam 3 Chapters 7 & 9

Templates and Vectors

G52CPP C++ Programming Lecture 15

The Standard Template Library Classes

Vectors. CIS 15 : Spring 2007

CPSC 427a: Object-Oriented Programming

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CS 103 Unit 12 Slides

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Exceptions, Templates, and the STL

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

The Standard Template Library. An introduction

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CS11 Advanced C++ Fall Lecture 1

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

Programming in C++: Assignment Week 1

STL Quick Reference for CS 241

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

6.096 Introduction to C++

Chapter 5. The Standard Template Library.

Programming with Haiku

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

STL: C++ Standard Library

Associative Containers and Iterators. Ali Malik

Exercise 9 Pointers, Iterators and Recursion

MODULE 33 --THE STL-- ALGORITHM PART I

Lecture-5. STL Containers & Iterators

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

COEN244: Class & function templates

Module 9. Templates & STL

Week 3: Pointers (Part 2)

C++ Scope Resolution Operator ::

Functions. Functions in C++ Calling a function? What you should know? Function return types. Parameter Type-Checking. Defining a function

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Due Date: See Blackboard

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

PIC 10A. Lecture 23: Intro to STL containers

CSE 100: C++ TEMPLATES AND ITERATORS

W3101: Programming Languages C++ Ramana Isukapalli

COMP322 - Introduction to C++

Stack memory - "scratch pad" memory that is used by automatic variables.

List, Stack, and Queues

Concurrent Data Structures in C++ CSInParallel Project

Container Class and Integrators, Proxy Class EC6301-OOPS AND DATA STRUCTURES

7 TEMPLATES AND STL. 7.1 Function Templates

Cours de C++ Introduction

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library

CSE 100: C++ TEMPLATES AND ITERATORS

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

PENN STATE UNIVERSITY Department of Economics

Lambda functions. Zoltán Porkoláb: C++11/14 1

Lab 8 The Stack (LIFO) Structure

STL. Prof Tejada Week 14

CSCE 110 PROGRAMMING FUNDAMENTALS

Name SECTION: 12:45 2:20. True or False (12 Points)

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

ECE250: Algorithms and Data Structures C++ Language Tutorial

EL2310 Scientific Programming

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

pointers + memory double x; string a; int x; main overhead int y; main overhead

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Lab 6. Out: Wednesday 9 March 2005

Sequential Containers. Ali Malik

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

CSE030 Fall 2012 Final Exam Friday, December 14, PM

! An exception is a condition that occurs at execution time and makes normal continuation of the program impossible.

CSCE 206: Structured Programming in C++

Transcription:

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

What the heck is STL???? Another hard to understand and lazy to implement stuff?

Standard Template Library The standard template library (STL) contains Containers Algorithms Iterators A container is a way that stored data is organized in memory, for example an array of elements. Algorithms in the STL are procedures that are applied to containers to process their data, for example search for an element in an array, or sort an array. Iterators are a generalization of the concept of pointers, they point to elements in a container, for example you can increment an iterator to point to the next element in an array

Containers, Iterators, Algorithms Algorithms use iterators to interact with objects stored in containers Container Container Iterator Algorithm Objects Iterator Iterator Algorithm Iterator Algorithm

Containers A container is a way to store data, either built-in data types like int and float, or class objects The STL provides several basic kinds of containers <vector> : one-dimensional array <list> : double linked list <deque> : double-ended queue <queue> : queue <stack> : stack <set> : set <map> : associative array

Vector Container int array[5] = {12, 7, 9, 21, 13 ; vector<int> v(array,array+5); 12 7 9 21 13 v.pop_back(); 12 7 9 21 v.push_back(15); 12 7 9 21 15 0 1 2 3 4 12 7 9 21 15 v.begin(); v[3]

// function template declaration and definition template <class any_data_type> any_data_type MyMax(any_data_type Var1, any_data_type Var2) { return Var1> Var2? Var1 : Var2; int main(void) { cout << "MyMax(10,20) = " << MyMax(10, 20) << endl; cout << "MyMax('Z','p') = " << MyMax('Z', 'p') << endl; cout << "MyMax(1.234,2.345) = " << MyMax(1.234, 2.345) << endl; // some logical error here? cout << "\nlogical error, comparing pointers instead of string..." << endl; char* p = "Function"; char* q = "Template"; cout << "Address of *p = " << &p << endl; cout << "Address of *q = " << &q << endl; cout << "MyMax(\"Function\",\"Template\") = " << MyMax(p, q) << endl; cout << "Should use Specialization, shown later..." << endl; getch();

This is a STL // function template declaration and definition template <class any_data_type> any_data_type MyMax(any_data_type Var1, any_data_type Var2) { return Var1> Var2? Var1 : Var2;

This is a where you call the function from STL int main(void) { cout << "MyMax(10,20) = " << MyMax(10, 20) << endl; cout << "MyMax('Z','p') = " << MyMax('Z', 'p') << endl; cout << "MyMax(1.234,2.345) = " << MyMax(1.234, 2.345) << endl;

Back to definition The STL (Standard Template Library) represents a powerful collection of programming work that s been done well. It provides a group of containers, algorithms, and iterators, among other things.

Containers Any time you need to operate with many elements you require some kind of container. In native C (not C++) there was only one type of container: the array.

The problem is not that arrays are limited (though, for example, it s impossible to determine the size of array at runtime). Instead, the main problem is that many problems require a container with greater functionality.

For example, we may need one or more of the following operations: Add some string to a container. Remove a string from a container. Determine whether a string is present in the container. Return a number of distinct elements in a container. Iterate through a container and get a list of added strings in some order.

#include <iostream> #include <vector> #include <conio.h> using namespace std; int main(){ // create a vector to store int vector<int> vec; int i; // display the original size of vec cout << "vector size = " << vec.size() << endl; // push 5 values into the vector for (i = 0; i < 5; i++){ vec.push_back(i); // display extended size of vec cout << "extended vector size = " << vec.size() << endl; // access 5 values from the vector for (i = 0; i < 5; i++){ cout << "value of vec [" << i << "] = " << vec[i] << endl; // use iterator to access the values vector<int>::iterator v = vec.begin(); while (v!= vec.end()) { cout << "value of v = " << *v << endl; v++; getch();

#include <iostream> #include <vector> #include <conio.h> using namespace std; We declare a basic STL named vector int main() { // create a vector to store int vector<int> vec; int i;

Get the size of the vector vec // display the original size of vec cout << "vector size = " << vec.size() << endl; // push 5 values into the vector for (i = 0; i < 5; i++){ vec.push_back(i); Insert some value into vector vec

Print again the current vector vec size // display extended size of vec cout << "extended vector size = " << vec.size() << endl; // access 5 values from the vector for (i = 0; i < 5; i++){ cout << "value of vec [" << i << "] = " << vec[i] << endl; Just display the value inside vector vec

Use an iterator inside vector stl to access to values = similar to print it out using FOR loops // use iterator to access the values vector<int>::iterator v = vec.begin(); while (v!= vec.end()) { cout << "value of v = " << *v << endl; v++; getch(); Set initial value While vector vec is not the end of list yet

Iterators Iterators are pointer-like entities that are used to access individual elements in a container. Often they are used to move sequentially from element to element, a process called iterating through a container. vector<int> array_ 17 4 vector<int>::iterator size_ 4 23 12 The iterator corresponding to the class vector<int> is of the type vector<int>::iterator

Iterators The member functions begin() and end() return an iterator to the first and past the last element of a container vector<int> v array_ 17 4 23 v.begin() 12 v.end() size_ 4

Iterators One can have multiple iterators pointing to different or identical elements in the container vector<int> v array_ 17 4 23 12 size_ 4 i1 i2 i3

Iterators int max(vector<int>::iterator start, vector<int>::iterator end) { int m = *start; while (start!= stop){ if (*start > m) m = *start; ++start; return m; cout << max of v = << max(v.begin(), v.end());

int main() { vector <int> example; //Vector to store integers example.push_back(3); //Add 3 onto the vector example.push_back(10); //Add 10 to the end example.push_back(33); //Add 33 to the end for(int x=0; x<example.size(); x++) { cout<<example[x]<<" "; //Should output: 3 10 33 if(!example.empty()) //Checks if empty example.clear(); //Clears vector vector <int> another_vector; //Creates another vector to store integers another_vector.push_back(10); //Adds to end of vector example.push_back(10); //Same if(example==another_vector) //To show testing equality { example.push_back(20); for(int y=0; y<example.size(); y++) { cout<<example[y]<<" "; //Should output 10 20 getch();

Another examples: Please refer to extra powerpoint slide in my games programming site Or http://kengine.sourceforge.net/tutorial/g/stdmap-eng.htm http://learn.hackerearth.com/tutorial/programming/56/c-stlmap-and-multimap/ http://www.cprogramming.com/tutorial/stl/stlmap.html