. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.

Slides:



Advertisements
Similar presentations
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
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,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Generic programming starts with algorithms. Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Data Structures Using C++ 2E
Containers Overview and Class Vector
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ STL CSCI 3110.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Intro to the C++ STL Timmie Smith September 6, 2001.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
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.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Associative Containers Sets Maps Section 4.8. Associative Containers.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Software Design 2.1 STL concepts l Container: stores objects, supports iteration over the objects  Containers may be accessible in different orders 
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.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
The Standard C++ Library
The Standard C++ Library
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 9 – Sets and Maps 9.1 Associative Container
Programming with Concepts
18 – Sequential Containers
Associative Structures
CS212: Object Oriented Analysis and Design
Standard Template Library
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Standard Template Library
Presentation transcript:

. The Standard C++ Library

2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface

Concepts A concept is a list of properties of a type. STL defines a hierarchy of concepts for containers, iterators, and element types. Concepts for element types include: Concept B is a refinement of concept A if it imposes some additional requirements on A. (Similar to inheritance) LessThan Comparable - types with operator<,... Equality Comparable - types with operator==,... Assignable - types with operator= and copy Ctor

4 Main Components AlgorithmsContainersIterators Function Objects AdaptorsStreamsStrings

u Holds copies of elements. u Assumes elements have: Copy Ctor & operator = u The standard defines the interface. u Two main classes  Sequential containers: list, vector,....  Associative containers: map, set... Containers Assignable - types with operator= and copy Ctor

Sequential Containers u Maintain a linear sequence of objects

list - a linked list of objects  Efficient insertion/deletion in front/end/middle vector - an extendable sequence of objects  Efficient insertion/deletion at end  Random access deque – double-ended queue  Efficient insertion/deletion at front/end  Random access Sequential Containers

8 Containers Example - vector  Array of elements of type T  Random Access  Can grow on as needed basis std::vector v(2); v[0]= 45; v[1]= 32; v.push_back(60); std::vector v(2); v[0]= 45; v[1]= 32; v.push_back(60);

Associative Containers u Supports efficient retrieval of elements (values) based on keys. (Typical) Implementation: u red-black binary trees u hash-table (SGI extension, not standard)

Sorted Associative Containers Set u A set of unique keys Map u Associate a value to key (associative array) u Unique value of each key Multiset Multimap u Same, but allow multiple values

Sorted Associative Containers & Order u Sorted Associative containers use operator< as default order u We can control order by using our own comparison function u To understand this issue, we need to use function object Function Objects

Anything that can be called as if a function. For example: Pointer to function A class that implements operator() Advantage of class: u Enable accumulating information in the function object Function Objects

Example template class less { public: bool operator()(const T& lhs, const T& rhs) { return lhs < rhs; } }; less cmp; // declare an object if( cmp(1,2) ) … if( less ()(1,2) ) … Creates temporary objects, and then call operator()

Using Comparators template<typename T, typename Cmp = less > class set { … } … set s1; set > s2;// same type

Using Comparators template<typename T, typename Cmp = less > class set { … } … set s3; MyComp cmp; set s4(cmp); Creates a new MyComp object. Use given MyComp object.

u Iterators are allow to traverse sequences u Main Methods  operator*  operator++, and operator— u Different types of iterators - to support read, write and random access u Containers define their own iterator types u Changing the container can invalidate the iterator Iterators

Output IteratorInput IteratorTrivial IteratorForward IteratorBi-directional IteratorRandom-Access Iterator ++, input++, output++, I/O++, --, I/O Pointer arithmetic input Iterators

Iterator Types u Output: write only and can write only once u Input: read many times each item u Forward supports both read and write u Bi-directional support also decrement u Random supports random access (just like C pointer)

Iterators & Containers Bidirectional iterators: u list, map, set Random access iterators: u vector Input/output/forward iterators: u iostreams

Iterators and Containers Container c … Container::iterator i; for( i = c.begin(); i != c.end(); ++i) // do something with *i Container c … Container::iterator i; for( i = c.begin(); i != c.end(); ++i) // do something with *i class Container { … typedef … iterator; // iterator type of container iterator begin(); // first element of container iterator end(); // element after last of container

Iterators & Map Suppose we work with: map dictionary; map ::iterator i; … i = dictionary.begin(); What is the type of *i ?

Iterators & Map Every STL container type Container defines Container::value_type Type of elements stored in container  This is the type returned by an iterator Container::value_type Container::iterator operator*();

Iterators & Map u Ok, so what type of elements does a map return?  map keeps pairs  KeyType key – “key” of entry  ValueType value – “value” of entry

Pairs template struct pair { typedef T1 first_type; typedef T2 first_type; T1 first; T2 second; pair( const T1& x, const T2& y ) : first(x), second(y) {} };

Map value_type template< typename Key, typename T, typename Cmp = less > class map { public: typedef pair value_type; typedef Key key_type; typedef T mapped_type; typedef Cmp key_compare; };

Using map iterator map dict; … map ::iterator i; for( i = dict.begin(); i != dict.end(); i++ ) { cout first << “ “ second << “\n”; }

u Good functionality, wrong interface u For example, adaptors of basic containers u Limited interface: stack queue Adaptors

u Most STL algorithms works on sequences u Sequences are passed as two iterators:  beginning element  element one after last u Algorithms depend on iterator type not on container type pq sequence [p,q) Algorithms

Copy template Out copy(In first, In last, Out res) { while (first != last) *res++ = *first++; return res; }

template <class RandomAccessIterator, class StrictWeakOrdering> void sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp); Sort

Cryptic error messages

New solution in g++, define before including a standard library header: #define _GLIBCXX_CONCEPT_CHECKS

u SGI: u Dinkum: u Links to documentation

34 Main Components AlgorithmsContainersIterators Function Objects AdaptorsStreamsStrings

Mutable class String { char* _data; mutable int _len; public: String(const char* data) : _data(data),_len(-1){} void capitalie() { char* p =_data; while (*p) *p= toupper(*p++); } int length() const { if (_len==-1) _len= strlen(_data); return _len; } };

Inserters