Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.

Similar presentations


Presentation on theme: "Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects."— Presentation transcript:

1 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

2 Why Templates void swap(int& a, int& b) { int temp = a; a = b; b = temp; }

3 Templates allow common logic with different data types template void swap(C& a, C& b) { C temp = a; a = b; b = temp; }

4 STL Example vector 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());

5 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

6 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

7 Data structures/containers Vectors set lists dequeue

8 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

9 Map example Counting number of occurrences of each string

10 #include using namespace std; int main() { map string_count; string word; // input buffer for words. //--- Read words/tokens from input stream cout -D to end." << endl; while (cin >> word) { string_count[word]++; } //--- Write the count and the word. cout << " --- Word counts ---" << endl; map ::const_iterator iter; for (iter=string_count.begin(); iter != string_count.end(); ++iter) { cout second first << endl; } return 0; }//end main

11 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

12 Types of STL Algorithms Non-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_shuffle partition

13 Memory Management Typing allows templated functions to be efficient (almost always better than hand-written functions) Surprisingly easy to manage memory

14 Vector Memory Optins void myfunction () { vector v1(100); // set size to 100 vector v2(100); v2.resize(200); // now storage and size are 200 vector 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

15 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


Download ppt "Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects."

Similar presentations


Ads by Google