150 likes | 293 Views
Standard Template Library. C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects or types to be declared by the programmer sort, find, sets, looping. Why Templates. void swap(int& a, int& b) { int temp = a;
E N D
Standard Template Library • C++ introduced both object-oriented ideas, as well as templates to C • Templates are ways to write general code around objects or types to be declared by the programmer • sort, find, sets, looping
Why Templates void swap(int& a, int& b) { int temp = a; a = b; b = temp; }
Templates allow common logic with different data types template <class C> void swap(C& a, C& b) { C temp = a; a = b; b = temp; }
STL Example vector<int> v(3); // Declare a vector of 3 elements. v[0] = 7; v[1] = v[0] + 3; v[2] = v[0] + v[1]; reverse(v.begin(), v.end());
Advantages of using templated containers • Common code can be written just once with basic data structures • Algorithms on basic structures can be implemented generally (and efficiently) • Standard data processing tasks can be reduced to calls to the standard template library • allows programmer to focus on value-add parts of code
Comparison to Java • In Java, general classes like Vector and List can be programmed • Not typed: any object can be put in a vector • leads to possible errors in the code • No optimization possible based on type • knowing both the container and data type at compile time, the best-suited algorithm can be chosen
Data structures/containers • Vectors • set • lists • dequeue
Hashes (map in STL) • Less known than other containers • Work like instant telephone book lookups, or a generic array • Input and Output can be of any type • e.g. string -> phone number • Access is very fast: log(n) on average
Map example • Counting number of occurrences of each string
#include <map> #include <string> #include <iostream> using namespace std; int main() { map <string, int> string_count; string word; // input buffer for words. //--- Read words/tokens from input stream cout << "Enter words, pressing enter in between. Press <ctrl>-D to end." << endl; while (cin >> word) { string_count[word]++; } //--- Write the count and the word. cout << " --- Word counts ---" << endl; map<string, int>::const_iterator iter; for (iter=string_count.begin(); iter != string_count.end(); ++iter) { cout << iter->second << " " << iter->first << endl; } return 0; }//end main
Functions on standard objects • Data structures aren't much good without functions that work on them • Generic algorithms can often take any flavour of container • sorting works on vectors, lists, queues • particular algorithm will take advantage of appropriate aspects of container • sorting vectors is faster than sorting linked lists
Types of STL Algorithms • Mutating • sort • copy • transform • replace • fill • generate • reverse • random_shuffle • partition • Non-Mutating • for_each • find • find_if • adjacent_find • find_first_of • count • count_if • mismatch • equal
Memory Management • Typing allows templated functions to be efficient (almost always better than hand-written functions) • Surprisingly easy to manage memory
Vector Memory Optins void myfunction () { vector<int> v1(100); // set size to 100 vector<int> v2(100); <..find out you need 200 elements> v2.resize(200); // now storage and size are 200 vector<int> v3; v3.reserve(100); // make room for 100, but size=0 for (i = 1; i < 150; i++) { v3.push_back(i); } } In all cases, memory in vector is freed at end of function
Learning the STL • The standard template library is not the easiest thing to understand • Simple examples cut+paste is definitely the way to start • Best done after some C++ programming experience, reading • template compiler errors are among the most cryptic I've ever seen • Need to be able to "think like a compiler" to interpret them