Programming with ANSI C ++

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
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++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Templates and the STL.
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
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
 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.
Intro to the C++ STL Timmie Smith September 6, 2001.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
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.
Final Exam Review COP4530.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Standard Template Library
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
Iterators and Sequences
Understanding Algorithms and Data Structures
Regarding homework 9 Many low grades
Introduction to Computers Computer Generations
Standard Template Library
C++ Templates.
Lectures linked lists Chapter 6 of textbook
Programming with ANSI C ++
Data Structure and Algorithms
Sequences and Iterators
C++ Standard Library.
Standard Template Library (STL)
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Starting Out with C++ Early Objects Eighth Edition
The Bag and Sequence Classes with Linked Lists
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Programming with ANSI C ++
Associative Structures
Discussion section #2 HW1 questions?
CS212: Object Oriented Analysis and Design
Final Exam Review COP4530.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Programming with ANSI C ++
Constructors and Other Tools
Standard Template Library Model
Iterators and STL Containers
Standard Template Library
STL List.
Software Design Lecture : 39.
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
SPL – PS1 Introduction to C++.
STL List.
Presentation transcript:

Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Chapter 15 Standard Template Library (STL)

The STL STL is Standard Template Library, a collection of generic software component (generic containers) and generic algorithms, glued by objects called Iterators. STL is a different type of library. It has quite a large number of non member functions designed to work on multiple classes of container types.

The non member advantage Feasible to implement all operations in most efficient manner many useful algorithms like find(), replace(), merge(), sort() are implemented in STL Being non member function they can be used with any container even a newly designed one

The generic software components These containers are classes which can in turn contain other objects. Sequence containers Vector, List, DeQueue Sorted associative containers Set, Map, Multi-set, Multi-map Adapted containers Stack , Queue

The Iterators These are pointer like objects They are categorized into various categories unlike pointers. The generic algorithms are written to work on iterators objects rather then any data structure. Containers are also designed in terms of iterators rather then normal pointers.

Types of Iterators Find algorithm works on input Iterators sort is defined to require Random Access Iterators list (the doubly linked list) container is defined to have Bi-directional Iterator Thus sort can not be used with list!

Generic programming Idea of Generic Programming has nothing to do with C++! It is a mechanism of designing generic software components like Vector, list, deque and so on. Unlike normal programming practice, Generic algorithms are not designed having any software component in mind.

Generic programming Designed keeping in mind minimum level of requirements. Can work with many variants of different software components The software components (containers) and algorithms are connected to each other by Iterators as mentioned earlier

The object based model The inefficiency of object oriented model The model presented by STL offers similar advantages with more efficient architecture The generality achieved here is by templates, Iterators and good design rather then having class hierarchy 5/26/2018 The C2C Programme

The Containers STL provides tested and debugged software components available readily. They are reusable in the sense that we can use them as a building block for any other software development projects.

Advantages of containers Small in number Generality Efficient, Tested, debugged and standard Portability and reusability Automatically adjusting growth Similar behavior to built-in types Extensibility

Advantages of Generic Algorithms readymade from STL. Using the best of the breed mechanisms to be as efficient as possible Standardized so have more acceptability then proprietary algos Similar semantics are designed for all these algorithms.

Iterators We need efficient methods to traverse these containers Iterators provide us the way to do that The Functional interface and pointer like interface The array to be accessed using pointers The get() and put() used by files

Iterators The Iterators are provided by the STL designers for finer control. They are not only provided, they are open to the programmer

Iterators Programmers can define and use Iterators like pointers to the component objects, dereference them assign them and so on like pointers.

Why not Pointers? The reason is that although in most of the cases the iterators are implemented as pointers, one can choose the more fitting implementation for a special case.

The Efficiency of Iterators Open design:- having Iterators open to programmers to manipulate Address manipulation by which the job is possible to be done in minimum steps We can have additional checks possible before applying algorithm to contents like checking to see if the sort algorithm is provided with Random Access Iterators and nothing else

The Vector #include <vector> vector <Student> vs; vector <int> vi; vi.push_back(12); //inserting at the end of vector! Student Lara(1,"Brian Charles Lara"); vs.push_back(Lara);

The List operations Implemented using doubly linked list. Insertion at random place is most efficient constant time operation at every location in the list. Vector has only constant time operation in the end deque in the beginning and at the end and not at every place

The Dequeue deque <Student> ds; ds.push_front(Lara);

The Sorted Associative Containers Three is a need for a container which always remains sorted. All database indexes are examples of such container. Every element inserted in sorted order automatically object which is inserted must obey some form of ordering using operator <() const 5/26/2018 The C2C Programme

The Map It is a container which can have two items, a key and a value stored in itself. It is automatically sorted on the key item at the time of insertion The key item must have some form of ordering. For a user defined objects, it is important to overload operator < as const to have ordering

The Set The set is collection of keys only in sorted form. When we are interested only in verifying if a key is a valid we can use sets We can insert all keys and when the key is referenced we can see if key belongs to inserted data or not.

Algorithms: Find int *pos = find(IntArray, IntArray+6, 3); StudentIndex = find(vs.begin(),vs.end(), Maradona); ListIndex = find(ls.begin(),ls.end(),Tiger); DeqIndex = find(ds.begin(),ds.end(),Steffi); Find returns same type of Iterator which is passed as first and second argument. 5/26/2018 The C2C Programme

Algorithms: Find Find() can work with all the types of containers because the Iterators expected are of category called Input Iterators which are supported by all containers.

Algorithm: Copy copy(vs.begin(),vs.end(),AnotherVector.begin()); copy(vs.begin(),vs.end(),ds.begin()) copy(vs.begin(),vs.end(),ls.begin()); copy has three arguments. The first two arguments are same type of iterators indicating a range of values. The third Iterator can be of different type then type of first two Iterators