15
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

Standard Template Library

Embed Size (px)

DESCRIPTION

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; - PowerPoint PPT Presentation

Citation preview

Page 1: Standard Template Library

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

Why Templates

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

Page 3: Standard Template Library

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

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

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

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

Data structures/containers

Vectors

set

lists

dequeue

Page 8: Standard Template Library

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

Map example

Counting number of occurrences of each string

Page 10: Standard Template Library

#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

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

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

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

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

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