1 / 15

Standard Template Library

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;

Télécharger la présentation

Standard Template Library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 <class C> void swap(C& a, C& b) { C temp = a; a = b; b = temp; }

  4. 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());

  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 <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

  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 • 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

  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<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

  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

More Related