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