Download - Standard Template Library
![Page 1: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/1.jpg)
Standard Template Library
C++ introduced both object-oriented ideas, as well as templates to CTemplates are ways to write general code around objects or types to be declared by the programmer sort, find, sets, looping
![Page 2: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/2.jpg)
Why Templates
void swap(int& a, int& b) { int temp = a; a = b; b = temp;}
![Page 3: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/3.jpg)
Templates allow common logic with different data types
template <class C>void swap(C& a, C& b) { C temp = a; a = b; b = temp;}
![Page 4: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/4.jpg)
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());
![Page 5: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/5.jpg)
Advantages of using templated containers
Common code can be written just once with basic data structuresAlgorithms 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
![Page 6: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/6.jpg)
Comparison to Java
In Java, general classes like Vector and List can be programmedNot 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
![Page 7: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/7.jpg)
Data structures/containers
Vectors
set
lists
dequeue
![Page 8: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/8.jpg)
Hashes (map in STL)
Less known than other containersWork like instant telephone book lookups, or a generic arrayInput and Output can be of any type e.g. string -> phone number
Access is very fast: log(n) on average
![Page 9: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/9.jpg)
Map example
Counting number of occurrences of each string
![Page 10: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/10.jpg)
#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
![Page 11: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/11.jpg)
Functions on standard objects
Data structures aren't much good without functions that work on themGeneric 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
![Page 12: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/12.jpg)
Types of STL AlgorithmsNon-Mutating for_each find find_if adjacent_find find_first_of count count_if mismatch equal
Mutating sort copy transform replace fill generate reverse random_shuffl
e partition
![Page 13: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/13.jpg)
Memory Management
Typing allows templated functions to be efficient (almost always better than hand-written functions)Surprisingly easy to manage memory
![Page 14: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/14.jpg)
Vector Memory Optinsvoid 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=0for (i = 1; i < 150; i++) { v3.push_back(i);}
}
In all cases, memory in vector is freed at end of function
![Page 15: Standard Template Library](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812ae5550346895d8ecbe0/html5/thumbnails/15.jpg)
Learning the STL
The standard template library is not the easiest thing to understandSimple examples cut+paste is definitely the way to startBest 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