Standard Template Library

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

Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
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, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Basic C++ Sequential Container Features
Data Structures Using C++ 2E
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
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.
 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.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Lecture 7 : Intro. to STL (Standard Template Library)
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
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.
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library
CS212: Object Oriented Analysis and Design
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
Course Developer/Writer: A. J. Ikuomola
Top 50 Data Structures Interview Questions
Programming with ANSI C ++
Lectures linked lists Chapter 6 of textbook
Data Structure and Algorithms
Data Abstraction & Problem Solving with C++
UNIT-3 LINKED LIST.
Standard Template Library (STL)
Data Structures Interview / VIVA Questions and Answers
Program based on pointers in C.
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
priority_queue<T>
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Data Structures & Algorithms
Standard Template Library
STL List.
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
The List Container and Iterators
STL List.
Standard Template Library
Presentation transcript:

Standard Template Library It is a collection of generic software components and generic algorithms, glued by objects called iterators. It has a large number of non-member functions designed to work on multiple classes of container types. This non-member functions are known as generic algorithms. STL has generic components(containers) which are classes that can, in turn, contain other objects. Iterators are pointer like objects.

Generic software components(containers) STL provides tested and debugged components readily available. They are reusable as a building block for other software development projects. These generic software components are also called containers as they are basically collection of other objects.

Following are advantages of these components: Small in number Generality Efficient, tested, debugged and standardized Portability and reusability Automatically adjusting growth Similar behaviour to built-in types Extensibility There are three types of containers Sequence container(vector,list,deque) Associative sorted container(map,multimap, set,multiset) Adaptive container(stack, queue)

Generic Algorithms The algorithms which operate on the software components are designed in such a way that they depend very little on the data structure of the component. We can write our own algorithms in place of generic algorithms. STL contains so many useful algorithms like find(), replace(), merge(), sort(), etc. Being general these algorithms are non-member functions and can be used with all the containers.

Iterators Iterators provide a way to traverse the containers. Programmers can not only define and use iterators like pointers to the component objects, they can dereference them, assign them and so on like pointers. The efficiency of the STL iterators depends heavily on two principles: Open design having iterators open for programmers to manipulate. Address manipulation by which the job is possible to be done in minimum steps.

List Class The list class is used to create sequential containers. Elements of list are single objects. The elements of list occupy non-contiguous memory. They are doubly-linked through a pair of pointers. One of the pointers points at the next element and other points to the previous element of the list. List allows both forward and backward traversal.

For using list template class, the header file #include<list> needs to be included in the source code. Declaration of the objects of list class: list <char>clist; //creating list of character list<int>ilist; list <int> ilist(3); // number of elements would have can be specified. list<int> ilist(3,0); //initialize all elements with 0 Array of list: int arr[5] = {1,2,3,4,5}; list<int>ilist(arr,arr+5);

Member functions: list<>::push_front() list<int>Ilist Ilist.push_front(4); Ilist.push_front(7); //inserts 7 at the beginning List<>::push_back() ilist.push_back(2); ilist.push_back(3);//inserts 3 at the end of the list List<>::pop_front() & list<>::pop_back() ilist.pop_front(); ilist.pop_back();

Traversing a list using iterator: list<int>::iterator iter; for(iter = ilist.begin(); iter != ilist.end(); ++iter) { cout<<*iter; } list<>::insert() function: Ilist.insert(ilist.end(),67); Ilist.insert(ilist.begin(),12); list<>:: size() function: cout<<ilist.size() // gives number of elements of list

Find() function: iter = find(ilist.begin(),ilist.end(),67); ilist.insert(iter,78); Find function searches for the value 67 from the beginning of the list to the end. It inserts value 78 before 67 in the list, if the value is found. Else, it appends 78 at the end of the list. i.e. if value is found in the list, it returns current iterator, else it returns the value of the list::end() function.

List::erase function: this function enables random deletion from the list. iter = find(ilist.begin(),ilist.end(),23); if(iter!=ilist.end()) ilist.erase(iter); List::clear() function this function erase all elements in the list. ilist.clear(); List::empty() function this function is used to test whether a list is empty or not. if(ilist.empty) {} else {}

Vector class For using template class, the header file vector needs to be included in the source code. A vector does not actually regrow itself with each individual insertion. The amount of memory a vector captures is larger than the number of elements it actually stores. when this storage becomes full, it again regrows itself by a certain amount of accommodate the latest insertion. the amount by which a vector regrows differs from compiler to compiler.

The names of the member functions of the vector class are the same as those of the list class. The vector class has two functions that enable us to find the capacity and size of the vector. These are vector::capacity() and vector::size(). Capacity is the total size of the block currently captured by a vector. And size is number of elements actually stored in the memory block. In vector elements are stored in contiguous memory (just like an array).

Insertion into and deletion from an intermediate position in a vector is inefficient. This is because for such operations all elements starting from the insertion point need to be pushed up or pushed down. random access to a particular element is efficient. For traversing to the element that has our desired value, only the internal iterator has to be incremented since the elements are in the contiguous block of memory.

Deque class It is a useful container adapted into the two other containers-the stack and the queue. Deque is a structure where insertion in the end as well as insertion in the front is possible to be done with constant time. ds.push_front(4); this statement is possible with only deque, vector and list only, for other containers it gives compile time error.

Sorted associative containers Sorted associative containers are containers where the content is always sorted; irrespective of the order of insertions and deletions. every element, when inserted, is inserted, is inserted at the exact place where it is to be inserted in the sorted order. we can achieve that ordering overloading operator <() const for each of the objects. these containers are associative and an element of the record can be used to access the entire record.

The difference between a sequence container and sorted associative container is that sequence container element is accessed by index or position of element while sorted associative container element is accessed by the value of key, which is a part of element. Sorted associative container also allows sequential operations.