Standard Template Library

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

Standard Containers: Vectors
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
More on the STL vector list stack queue priority_queue.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
C++ for Engineers and Scientists Third Edition
Basic C++ Sequential Container Features
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
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.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Lecture 7 : Intro. to STL (Standard Template Library)
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
CS212: Object Oriented Analysis and Design
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Programming with ANSI C ++
Standard Template Library
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Introduction to Linked Lists
structures and their relationships." - Linus Torvalds
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
priority_queue<T>
CS212: Object Oriented Analysis and Design
Recursive Linked List Operations
Chapter 17: Linked Lists.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
By Yogesh Neopaney Assistant Professor Department of Computer Science
Standard Template Library
STL List.
the Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
structures and their relationships." - Linus Torvalds
The List Container and Iterators
SPL – PS1 Introduction to C++.
STL List.
A dictionary lookup mechanism
Presentation transcript:

Standard Template Library The STL is a large part of the C++ Standard Library Major components data structures (containers/collections for data – CS2851) vector (array), list, deque, stack, queue, set, … algorithms Common operations (functions) that can be applied to many STL containers iterators link the algorithms to the data structures. iterators are designed for efficient traversal through the data structures. Similar to pointers Fall 2005 CS-1030 Dr. Mark L. Hornick

Linked Lists – the list class Arranges data in non-contiguous blocks Links them together to create order Stored Object Link to Previous Cell Link to Next Cell Fall 2005 CS-1030 Dr. Mark L. Hornick

list is a Doubly Linked List Last First Size Main List Cell Data Data … Data … Fall 2005 CS-1030 Dr. Mark L. Hornick

list in C++ (STL) Syntax #include <list> using namespace std; list<T> collection; // T: datatype stored in vector Fall 2005 CS-1030 Dr. Mark L. Hornick

list Operations Adding elements Removing an element push_back – At the end push_front – At the beginning insert – Anywhere Removing an element pop_back – From the end pop_front – From the beginning erase, remove - Anywhere Fall 2005 CS-1030 Dr. Mark L. Hornick

list Operations (2) Information accessors/inspectors Modification size – How many? empty – Are there none? Modification sort – NOT part of <algorithm> reverse – change the order Fall 2005 CS-1030 Dr. Mark L. Hornick

The vector class vector is a “smart” array Like the Java ArrayList Storage is in consecutive memory locations Location = start + index*size Very little storage overhead Only need the start and object size 1 … n-1 vector start  Fall 2005 CS-1030 Dr. Mark L. Hornick

vectors in C++ (STL) User-specified data type is stored Syntax: #include <vector> using namespace std; vector<T> collection; // T: datatype stored in vector // Like Java’s ArrayList<T> Fall 2005 CS-1030 Dr. Mark L. Hornick

vector Operations Adding elements: Removing an element push_back – At the end push_front – At the beginning insert – Anywhere Removing an element pop_back – From the end pop_front – From the beginning erase, remove - Anywhere Fall 2005 CS-1030 Dr. Mark L. Hornick

lists and vectors usage Example Fall 2005 CS-1030 Dr. Mark L. Hornick

lists vs. vectors Advantages Disadvantages Flexible storage Grows easier Ultimately more data can be stored Disadvantages O(n) access time Loss of indexing Storage overhead is higher Fall 2005 CS-1030 Dr. Mark L. Hornick

Iterators A mechanism for sequentially accessing STL containers More important for list than vector Vector can use indexing for access (e.g. x[i]) Automatically included as part of the container Helps describe the next and previous links Fall 2005 CS-1030 Dr. Mark L. Hornick

Iterator Notation Declaration Operators list<TYPE>::iterator NAME; Operators * Access data element (dereferencing) ++,-- Advance or retreat in the list ==,!= Comparison NOTE: No < or > Fall 2005 CS-1030 Dr. Mark L. Hornick

Iterators in a Linked List Main List Cell .begin() Size First Last Data Data … … Data … .end() Fall 2005 CS-1030 Dr. Mark L. Hornick

Iterator Variations Standard iterators can be used to change a list *iter = newValue; Special iterators for const lists (can’t change) list<TYPE>::const_iterator citer; Reverse iterators list<TYPE>::reverse_iterator riter; list<TYPE>::const_reverse_iterator criter; Fall 2005 CS-1030 Dr. Mark L. Hornick

More on Reverse Iterators For going from end to beginning ++ Retreats -- Advances Bounds rbegin() – Last rend() – One “before” the first Fall 2005 CS-1030 Dr. Mark L. Hornick

Other iterator Member Functions These member functions need iterators insert – Where to insert erase – Which one to erase find – Locate the item that was found Iterators also apply to vectors Usage is the same vector<TYPE>::iterator NAME; Fall 2005 CS-1030 Dr. Mark L. Hornick

STL Algorithms An algorithm is an operation (function) that can be applied to many STL containers The STL algorithms are generic - they can operate on a variety of data structures. STL container classes such as vector and list Program-defined data structures Behavior must satisfy the requirements of a particular algorithm STL algorithms achieve generality by accessing and traversing the elements of a container indirectly through iterators. Iterators must be settable and dereferencable Fall 2005 CS-1030 Dr. Mark L. Hornick

Sort Algorithm The sort algorithm is an operation (function) that can be applied to many STL containers notable exception: list container (has it’s own method) sort() orders a container's contents in ascending order as defined by the operator<() as applied to the container’s elements Programmer-defined types can be sorted just as easily as a built-in type Fall 2005 CS-1030 Dr. Mark L. Hornick

find Algorithm searches a subrange of the elements in a container (or all the elements), looks for an element that is "equal to" a specified value; stops when it finds the first element equal to the specified value the equality operator (==) must be defined for the type of the container's elements. search value must be of the same type as the elements stored in the container or of a type that the compiler can automatically convert. Return value is an iterator specifying the position of the first matching element. If no matching element is found, the return value is equal to the iterator specifying the end of the element subrange Fall 2005 CS-1030 Dr. Mark L. Hornick