The Standard Template Library

Slides:



Advertisements
Similar presentations
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Advertisements

Data Structures Using C++ 2E
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
Writing Your Own STL Container Ray Lischner
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ STL CSCI 3110.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 7 : Intro. to STL (Standard Template Library)
The Standard Template Library Container Classes Version 1.0.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Final Exam Review COP4530.
Object-Oriented Programming (OOP) Lecture No. 41
Motivation for Generic Programming in C++
Standard Template Library
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Programming with ANSI C ++
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Basic Data Structures.
STL Common tools for C++.
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Basic Data Structures.
Chapter 22: Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Associative Structures
Final Exam Review COP4530.
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Containers, Iterators, Algorithms, Thrust
Elements are always copied when they are put into a container
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
SPL – PS1 Introduction to C++.
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

The Standard Template Library

Books on standard C++ library 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) (Grębosz J.: Pasja C++, RM, W-wa) Other (mentioned on Lecture Nr 1)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

STL Algorithms copy (coll1.begin(), coll1.end(), coll2.begin()); end of range given only for first range sizes of ranges must suffice algorithm does elementary copy only 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) setting initial collection size is easy for some (sequential) container classes

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

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

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

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

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

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

Function object as algorithm argument function objects behave like functions, but using operator()() example: foreach2.cpp are objects easily optimizable by compiler 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

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

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

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

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