THE STANDARD EMPLATE L (STL)

28
THE S TANDARD TEMPLATE LIBRARY (STL) Week 6 – BITE 1513 Computer Game Programming

Transcript of THE STANDARD EMPLATE L (STL)

THE STANDARD TEMPLATELIBRARY (STL)

Week 6 – BITE 1513Computer 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

Container

AlgorithmIterator

Container

Iterator

Algorithm

Objects

Iterator

Iterator

Algorithm

Algorithms use iterators to interact with objectsstored in containers

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

12 7 9 21 13

int array[5] = {12, 7, 9, 21, 13 };vector<int> v(array,array+5);

v.begin();

12 7 9 21

v.push_back(15);

12 7 9 21 15

12 7 9 21 15

v[3]

0 1 2 3 4

v.pop_back();

// function template declaration and definitiontemplate <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();}

// function template declaration and definitiontemplate <class any_data_type>any_data_type MyMax(any_data_type Var1, any_data_type Var2) {return Var1> Var2 ? Var1 : Var2;}

This is a 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;

This is a where you call the function from STL

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.

ContainersAny 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 intvector<int> vec;int i;

// display the original size of veccout << "vector size = " << vec.size() << endl;

// push 5 values into the vectorfor (i = 0; i < 5; i++){vec.push_back(i);}

// display extended size of veccout << "extended vector size = " << vec.size() << endl;

// access 5 values from the vectorfor (i = 0; i < 5; i++){cout << "value of vec [" << i << "] = " << vec[i] << endl;}

// use iterator to access the valuesvector<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;

int main(){

// create a vector to store intvector<int> vec;int i;

We declare a basic STL named vector

// display the original size of veccout << "vector size = " << vec.size() << endl;

// push 5 values into the vectorfor (i = 0; i < 5; i++){vec.push_back(i);}

Get the size of the vector vec

Insert some value into vector vec

// display extended size of veccout << "extended vector size = " << vec.size() << endl;

// access 5 values from the vectorfor (i = 0; i < 5; i++){cout << "value of vec [" << i << "] = " << vec[i] << endl;}

Print again the current vector vec size

Just display the value inside vector vec

// use iterator to access the valuesvector<int>::iterator v = vec.begin();while (v != vec.end()) {cout << "value of v = " << *v << endl;v++;}

getch();}

Use an iterator inside vector stl to access to values = similar to print it out using FOR loops

Set initial value

While vector vec is not the end of list yet

vector<int>array_

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.

1742312

size_ 4

vector<int>::iterator

The iterator corresponding tothe class vector<int> is ofthe 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> varray_ 17

42312

size_ 4

v.end()

v.begin()

Iterators• One can have multiple iterators pointing to different or

identical elements in the container

vector<int> v

array_ 1742312

size_ 4i3

i1

i2

Iteratorsint 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 integersexample.push_back(3); //Add 3 onto the vectorexample.push_back(10); //Add 10 to the endexample.push_back(33); //Add 33 to the endfor(int x=0; x<example.size(); x++) {

cout<<example[x]<<" "; //Should output: 3 10 33}if(!example.empty()) //Checks if empty

example.clear(); //Clears vectorvector <int> another_vector; //Creates another vector to store integersanother_vector.push_back(10); //Adds to end of vectorexample.push_back(10); //Sameif(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 powerpointslide in my games programming site

Or

http://kengine.sourceforge.net/tutorial/g/stdmap-eng.htm

http://learn.hackerearth.com/tutorial/programming/56/c-stl-map-and-multimap/

http://www.cprogramming.com/tutorial/stl/stlmap.html