Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai.

Similar presentations


Presentation on theme: "The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai."— Presentation transcript:

1 The Standard Template Library

2 Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, examples: http://www.josuttis.com/libbook/examples.zip (Polish: Nicolai M. Josuttis: C++ Biblioteka standardowa Podręcznik Programisty, Helion 2003, examples: ftp://ftp.helion.pl/przyklady/cpbspp.zip) (Polish: Nicolai M. Josuttis: C++ Biblioteka standardowa Podręcznik Programisty, Helion 2003, examples: ftp://ftp.helion.pl/przyklady/cpbspp.zip) (Grębosz J.: Pasja C++, RM, W-wa) (Grębosz J.: Pasja C++, RM, W-wa) Other (mentioned on Lecture Nr 1) Other (mentioned on Lecture Nr 1)

3 Standard library of C++ language STL STL (The main part of standard library of C++ language) Stream classes Stream classes String classes String classes (library defined within std namespace)

4 Standard Template Library Main elements Main elements Generic data containers (lists, vectors, etc.) Generic data containers (lists, vectors, etc.) Iterators for browsing containers and providing an interface to containers for algorithms Iterators for browsing containers and providing an interface to containers for algorithms Algorithms operating on containers Algorithms operating on containers Other (to be discussed later...) Other (to be discussed later...) It is a template library! It is a template library! Data is generally separated from methods (where the OOP idea has gone?) Data is generally separated from methods (where the OOP idea has gone?) Designed for high-level programming Designed for high-level programming Designed for efficient programming Designed for efficient programming

5 BTW: algorithm’s complexity order O-notation O-notation find element in sorted array:O(log 2 (n)) find element in sorted array:O(log 2 (n)) find element in unsorted array: O(n) find element in unsorted array: O(n) quicksort:O(n · log 2 (n)) quicksort:O(n · log 2 (n)) bubblesort:O(n 2 ) bubblesort:O(n 2 )

6 Containers Sequential Sequential vector (dynamic table) vector (dynamic table) dequeue (double ende queue, using dynamic table) dequeue (double ende queue, using dynamic table) list (double linked one) list (double linked one) (arrays and strings not STL containers, but operable by STL algorithms) (arrays and strings not STL containers, but operable by STL algorithms) Associative Associative set (using binary tree) set (using binary tree) multiset multiset map, multimap map, multimap

7 Containers vector vector implemented as a dynamic table implemented as a dynamic table fast push_back() fast push_back() fast operator[] fast operator[] slow insert() slow insert()

8 Containers deque deque implemented using dynamic table implemented using dynamic table fast push_back() (here vector may be faster) fast push_back() (here vector may be faster) fast push_front() (not in vector template) fast push_front() (not in vector template) fast operator[] fast operator[] slow insert() slow insert()

9 Containers list list implemented using double linked list implemented using double linked list fast push_back() fast push_back() fast push_front() fast push_front() fast insert() fast insert() no operator[] no operator[]

10 Iterators Iterators behave like regular pointers... Iterators behave like regular pointers... * -> * -> ++-- ++-- ==!= ==!= = (vector, deq: -,, +(int) ) (vector, deq: -,, +(int) ) But work for all the containers! But work for all the containers! container.begin()// return iterator of first elem. container.begin()// return iterator of first elem. container.end()// return iterator next to last elem. container.end()// return iterator next to last elem. example: list2.cpp example: list2.cpp

11 Containers set set implemented using binary tree implemented using binary tree sorted at insert (by default using operator<() ) sorted at insert (by default using operator<() ) equivalent to math. sets equivalent to math. sets find() method find() method fast insert() fast insert() no push_back() no push_back() no push_front() no push_front() no operator[] no operator[]

12 Containers multiset multiset set that allows repetitions of the same value set that allows repetitions of the same value ordering within group of elements of the same value is undefined ordering within group of elements of the same value is undefined

13 Containers multimap multimap multiset of pairs: key, val – use make_pair() multiset of pairs: key, val – use make_pair()

14 Containers map map set of pairs: key, val – use make_pair() set of pairs: key, val – use make_pair() For map only: operator[key]() For map only: operator[key]() index by value (associative array) index by value (associative array)

15 STL Algorithms Operate on containers using iterator interface Operate on containers using iterator interface generic, but not as fast as containers’ methods generic, but not as fast as containers’ methods slow on some combinations of algorithm/container slow on some combinations of algorithm/container may operate on various containers at the same time may operate on various containers at the same time Only elementary, simple algorithms Only elementary, simple algorithms parametrizable by different iterators parametrizable by different iterators parametrizable by function objects and adaptors parametrizable by function objects and adaptors Not very intuitive (an euphemism) Not very intuitive (an euphemism)

16 STL Algorithms Example: algo1.cpp Example: algo1.cpp min_element// operator<() min_element// operator<() max_element// operator<() max_element// operator<() sort// operator<() sort// operator<() find// operator==() find// operator==() reverse// operator=() reverse// operator=() min_element (coll.begin(), coll.end()) min_element (coll.begin(), coll.end()) range is [ coll.begin(), coll.end() ) (example: find1.cpp) range is [ coll.begin(), coll.end() ) (example: find1.cpp) proper range definition is a programmer’s responsibility proper range definition is a programmer’s responsibility range end should be attainable by ++’ing of start range end should be attainable by ++’ing of start what if we’re not sure of what is beginning, what is end? what if we’re not sure of what is beginning, what is end?

17 STL Algorithms copy (coll1.begin(), coll1.end(), coll2.begin()); copy (coll1.begin(), coll1.end(), coll2.begin()); end of range given only for first range end of range given only for first range sizes of ranges must suffice sizes of ranges must suffice algorithm does elementary copy only algorithm does elementary copy only doesn’t check destination size (iteartors are intreface to elements, not to whole collection) doesn’t check destination size (iteartors are intreface to elements, not to whole collection) proper collection size is a programmer’s responsibility (example: copy2.cpp) proper collection size is a programmer’s responsibility (example: copy2.cpp) setting initial collection size is easy for some (sequential) container classes setting initial collection size is easy for some (sequential) container classes

18 STL Iterators sizes of ranges must suffice, or... sizes of ranges must suffice, or... copy (coll1.begin(), coll1.end(), inserter(coll2, coll2.begin()) );... or use inserter iterators ;) (example: copy3.cpp)... or use inserter iterators ;) (example: copy3.cpp) inserter for all containers, all can insert() inserter for all containers, all can insert() inserts before specified location inserts before specified location for associative containers location is a hint only for associative containers location is a hint only back_inserter for containers that can push_back() back_inserter for containers that can push_back() front_inserter for containers that can push_front() front_inserter for containers that can push_front()

19 STL Iterators stream iterators stream iterators behave like regular ones behave like regular ones have interface of regular ones have interface of regular ones operate on i/o streams (example: ioiter1.cpp) operate on i/o streams (example: ioiter1.cpp) istream_iterator (cin) istream_iterator (cin) ++iter for stream>>temp, *iter for retrieving temp ++iter for stream>>temp, *iter for retrieving temp istream_iterator () istream_iterator () end of stream iterator end of stream iterator

20 STL Iterators reverse iterators reverse iterators have interface of regular ones have interface of regular ones reverse regular behaviour (example: rter1.cpp) reverse regular behaviour (example: rter1.cpp) container.rbegin() is actually last element (not the one after last) container.rbegin() is actually last element (not the one after last) container.rend() is actually element before first one! container.rend() is actually element before first one! ++ is --, -- is ++, etc. ++ is --, -- is ++, etc.

21 STL Algorithms removing elements from container removing elements from container remove(...) algorithm remove(...) algorithm actually doesn’t remove container contents (example: remove1.cpp) actually doesn’t remove container contents (example: remove1.cpp) elementary operation of moving elements elementary operation of moving elements doesn’t know the container, knows elements doesn’t know the container, knows elements doesn’t work for associative containers doesn’t work for associative containers returns new end of range (next to last) returns new end of range (next to last) use method erase(...) to get rid of elements (example: remove2.cpp) use method erase(...) to get rid of elements (example: remove2.cpp) many versions many versions works for associative containers (example: remove3.cpp) works for associative containers (example: remove3.cpp)

22 Extending STL programmer is allowed (and encouraged) to extend STL functionality programmer is allowed (and encouraged) to extend STL functionality create new templates, or just classes/functions create new templates, or just classes/functions example: print.hpp example: print.hpp typename keyword denotes argument’s type/class typename keyword denotes argument’s type/class as opposed to „mutable” it is usefull as opposed to „mutable” it is usefull

23 Function as algorithm argument single argument functions single argument functions examples: foreach1.cpp, transform1.cpp examples: foreach1.cpp, transform1.cpp predicates predicates single argument and bool result (example: prime1.cpp) single argument and bool result (example: prime1.cpp) two arguments and bool result (example: sort1.cpp) two arguments and bool result (example: sort1.cpp)

24 Function object as algorithm argument function objects function objects behave like functions, but using operator()() behave like functions, but using operator()() example: foreach2.cpp example: foreach2.cpp are objects are objects easily optimizable by compiler easily optimizable by compiler may have class member variables, „internal state” passed by the constructor argument (example: add1.cpp) may have class member variables, „internal state” passed by the constructor argument (example: add1.cpp) we may have many function objects of the same class we may have many function objects of the same class

25 Function object as algorithm argument predefined function object templates predefined function object templates less<>, greater <> less<>, greater <> set s; defaults to set > s; we may also:set > s; we may also:set > s; negate<>, multiply<> // use in transform(...) algorithm negate<>, multiply<> // use in transform(...) algorithm

26 Function adaptors define special cases of function use define special cases of function use adapt function, when different interface (i.e. argument list) is required adapt function, when different interface (i.e. argument list) is required bind2nd(less (),50) (example: fo1.cpp) bind2nd(less (),50) (example: fo1.cpp) creates default second argument creates default second argument

27 Container elements interface required always interface required always copy constructor copy constructor assignment operator assignment operator destructor destructor interface required sometimes interface required sometimes default constructor default constructor equality operator == equality operator == comparison operator < comparison operator <

28 STL, errors and exceptions aimed at maximizing speed aimed at maximizing speed in case of improper use (*end()=something) behaviour is undefined, let’s hope it crashes in case of improper use (*end()=something) behaviour is undefined, let’s hope it crashes be carefull with iterators and ranges be carefull with iterators and ranges only minimal chcecks are done (bad_alloc exception) only minimal chcecks are done (bad_alloc exception) there is a debug version of the library there is a debug version of the library use it! use it! some methods of some containers are transaction-safe some methods of some containers are transaction-safe some are not! some are not! check the reference! check the reference!


Download ppt "The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai."

Similar presentations


Ads by Google