Data Structures Using C++ 2E

Slides:



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

Data Structures Using C++ 2E
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
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.
Standard Containers: Vectors
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Basic C++ Sequential Container Features
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
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:
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
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.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
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.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
The Standard Template Library Container Classes Version 1.0.
Standard Template Library (STL) - Use Vector and Deque
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.
C++ Review STL CONTAINERS.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
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.
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
CS212: Object Oriented Analysis and Design
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
CS212: Object Oriented Analysis and Design
Standard Template Library
Presentation transcript:

Data Structures Using C++ 2E Chapter 4 Standard Template Library (STL) I 1

Objectives Learn about the Standard Template Library (STL) Become familiar with the three basic components of the STL: containers, iterators, and algorithms (in Chapter 13) Explore how vector and deque containers are used to manipulate data in a program Discover the use of iterators Data Structures Using C++ 2E

Different C++/STL versions C++11 is the ISO C++ standard ratified in 2011. The previous standard is often referred to as C++98/C++03; the differences between C++98 and C++03 are very few. The list of STL classes in both C++ and C++98 can be found under http://www.cplusplus.com/reference/stl/ This materials of this textbook is based on C++ 98 Data Structures Using C++ 2E

Components of the STL Program’s main objective is to manipulate data and generate results Requires ability to store data, access data, and manipulate data STL components Containers Iterators: step through container elements Algorithms: manipulate data (e.g. sorting, search) Containers and iterators Class templates (types as class arguments) Data Structures Using C++ 2E

Container Types STL containers categories Sequence containers (sequential containers) Associative containers Container adapters Data Structures Using C++ 2E

Container Types STL containers categories Sequence containers (sequential containers) vector, deque, list… Associative containers set, map, multiset, multimap… Container adapters: adaptation of sequence or associative class containers by modifying and restricting its interface for some special purpose. stack (LIFO), queue (FIFO), priority_queue… Data Structures Using C++ 2E

Containers, Associated Header Files, and Iterator Support

STL containers From http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html

STL: Sequence containers Contiguous blocks of objects; elements can be inserted at any point in the sequence; sequences are indexed by integers Vector Fast insertion at end, and allow random access. List Fast insertion anywhere, but provide only sequential access. Deque Fast insertion at either end, and allow random access. Data Structures Using C++ 2E

STL: Associative containers Associative containers are a generalization of sequences; can be indexed by any type (e.g., an ID or a name) Implemented with a data structure called binary search tree, which will be covered later in this course. Data Structures Using C++ 2E

STL: Associative containers Sets: add, delete, search, and iterate through the set. Multisets: Multisets are just like sets, except that duplicate copies of the same element are allowed. Maps: Maps represent a mapping from one type (the key type) to another type (the value type). You can associate a value with a key, or find the value associated with a key, very efficiently; you can also iterate through all the keys. Multimaps: are just like maps except that there is no limit on elemenets with the same key. Data Structures Using C++ 2E

Sequence Containers Every object has a specific position Predefined sequence containers vector , deque , list Sequence container vector Logically: same as arrays Processed like arrays All containers Use same names (e.g. push_back()) for common operations May have specific operations Data Structures Using C++ 2E

Arrays Static arrays int foo[100]; Dynamic arrays Data Structures Using C++ 2E

Sequence Container: vector Vector container Stores, manages objects in a dynamic array Elements accessed randomly Time-consuming item insertion: middle, beginning Fast item insertion: end Class implementing vector container vector Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) Using a vector container in a program requires the following statement: #include <vector> Defining a vector container object Specify object type Example: vector<int> intlist; Example: vector<string> stringList; Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) Declaring vector objects TABLE 4-1 Various ways to declare and initialize a vector container

Declare and initialize a vector Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) Manipulating data stored in a vector sequence container Basic operations Item insertion Item deletion Stepping through the elements of a vector array Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) Difference: .at (index) checks if index is out of bound, while [index] doesn’t. Data Structures Using C++ 2E

Access a vector Data Structures Using C++ 2E

vector: size vs. capacity Size: the number of elements you have used. Capacity: allocated space. In STL, it’s doubled each time if additional space is needed. “double the size” is out of a careful design. Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) Function push_back Adds element to end of container Used when declaring vector container Specific size unknown Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) class vector provides various operations to process vector container elements Iterator The argument position in STL terminology Data Structures Using C++ 2E

Iterators Iterators provide a way of specifying a position in a container. The code vector<int> v; // add some integers to v vector::iterator i1 = v.begin(); vector::iterator i2 = v.end(); will create two iterators like this: Note: v.end() actually returns the past-the-end position Data Structures Using C++ 2E

Declaring an Iterator to a Vector Container Even though we can process vector like an array using array [ ] operator, sometimes we need to process vector elements based on their positions. class vector: function insert Insert element at a specific vector container position Uses an iterator class vector: function erase Remove element Data Structures Using C++ 2E

Declaring an Iterator to a Vector Container (cont’d.) class vector contains iterator (a typedef inside the class vector), declared as a public member of vector https://www.sgi.com/tech/stl/stl_vector.h typedef value_type* iterator; (nothing but a pointer!) If you want to declare a vector iterator, you’d need to use the scope resolution operator :: to specify which type of vector the iterator belongs to. Example vector<int>::iterator intVecIter; Data Structures Using C++ 2E

Declaring an Iterator to a Vector Container (cont’d.) vector<int>::iterator intVecIter; Container name (vector) Container data type (int) Scope resolution operator (::) ++intVecIter Advances iterator intVecIter to next element into the container *intVecIter Returns element at current iterator position Data Structures Using C++ 2E

Manipulation we can do with iteartors Using an iterator into a vector container Manipulating element type to be int Data Structures Using C++ 2E

Containers and the Functions begin and end Returns position of the first element into the container end() Returns position of (past) the last element into the container Functions have no parameters Data Structures Using C++ 2E

Example 4-4 Data Structures Using C++ 2E

Sequence Container: vector (cont’d.) TABLE 4-3 Various operations on a vector container Data Structures Using C++ 2E

Vector: other functions TABLE 4-3 Various operations on a vector container (cont’d.) http://www.cplusplus.com/reference/vector/vector/vector/ http://www.cplusplus.com/reference/vector/vector/resize/ Data Structures Using C++ 2E

Containers and the Functions begin and end (cont’d.) TABLE 4-4 Functions to determine the size of a vector container Data Structures Using C++ 2E

More examples of STL vector http://www.cplusplus.com/reference/stl/ http://www.cplusplus.com/reference/vector/vector/ https://www.sgi.com/tech/stl/Vector.html Data Structures Using C++ 2E

Member Functions Common to All Containers Constructors & destructor Default constructor, Several constructors with parameters, Copy constructor, Destructor Container operations: insert(), erase(), begin(), end(), etc… Containers operations: Assignment Comparison: equal to or not, >, < ,… Lexicographic order, for the entire container.

TABLE 4-5 Member functions common to all containers http://www.cplusplus.com/reference/vector/vector/max_size/ Data Structures Using C++ 2E

Member Functions Common to Sequence Containers TABLE 4-6 Member functions common to all sequence containers http://www.cplusplus.com/reference/vector/vector/insert/ Data Structures Using C++ 2E

The copy Algorithm Provides convenient way to output container elements Generic STL algorithm Usable with any container type and arrays Does more than output container elements Allows copying of elements from one place to another Function template copy definition Contained in header file algorithm Data Structures Using C++ 2E

copy examples http://www.cplusplus.com/reference/algorithm/copy/

Sequence Container: deque Deque: double-ended queue Implemented as dynamic arrays Can expand in either direction Class name defining deque container deque Header file deque contains Definition of the class deque Functions to implement various operations on a deque object Class deque contains several constructors http://www.cplusplus.com/reference/deque/deque/ https://www.sgi.com/tech/stl/Deque.html Data Structures Using C++ 2E

Sequence Container: deque (cont’d.) TABLE 4-7 Various ways to declare a deque object Data Structures Using C++ 2E

Sequence Container: deque (cont’d.) TABLE 4-8 Various operations that can be performed on a deque object Data Structures Using C++ 2E

More examples about STL deque http://www.cplusplus.com/reference/deque/deque/ https://www.sgi.com/tech/stl/Deque.html Data Structures Using C++ 2E

STL: Iterators in general Work like pointers Point to elements of a container (sequence or associative) Allow successive access to each container element Two most common operations on iterators ++ (increment operator) * (dereferencing operator) Examples ++cntItr; *cntItr; http://en.wikipedia.org/wiki/Increment_and_decrement_operators Data Structures Using C++ 2E

Types of Iterators Input iterators Output iterators Forward iterators Bidirectional iterators Random access iterators Data Structures Using C++ 2E

Operations supported by the iterators Read access; step forward int t = *inputIter; ++inputIter; write access; step forward *outputIter = t; ++outputIter; Both read/write; step forward Both read/write; step forward and backward. Supported in list, set, map, multiset, multimap Both read/write; jump forward and backward. rIterator = rIterator + n; Supported in vector deque, string, array

Input Iterators Read access Step forward element-by-element Return values element-by-element Provided for reading data from an input stream Data Structures Using C++ 2E

Input Iterators (cont’d.) TABLE 4-9 Operations on an input iterator Data Structures Using C++ 2E

Output Iterators Write access Step forward element-by-element Used for writing data to an output stream Cannot be used to iterate over a range twice Data Structures Using C++ 2E

Output Iterators (cont’d.) TABLE 4-10 Operations on an output iterator Data Structures Using C++ 2E

Forward Iterators Combination of All of input iterators functionality and almost all output iterators functionality Can refer to same element in same collection Can process same element more than once Data Structures Using C++ 2E

Forward Iterators (cont’d.) TABLE 4-11 Operations on a forward iterator Data Structures Using C++ 2E

Bidirectional Iterators Forward iterators that can also iterate backward over the elements Operations defined for forward iterators applicable to bidirectional Iterators To step backward Decrement operations also defined for biDirectionalIterator Can be used only with containers of type: vector, deque, list, set, multiset, map, and multimap Data Structures Using C++ 2E

Bidirectional Iterators (cont’d.) TABLE 4-12 Additional operations on a bidirectional iterator Data Structures Using C++ 2E

Random Access Iterators Bidirectional iterators that can randomly process container elements Can be used with containers of type: vector , deque , string, and arrays Operations defined for bidirectional iterators applicable to random access iterators Data Structures Using C++ 2E

Random Access Iterators (cont’d.) TABLE 4-13 Additional operations on a random access iterator Data Structures Using C++ 2E

Iterators (cont’d.) typedef iterator Every container (sequence or associative) contains a typedef iterator Iterator into a container declared using typedef iterator. For example Must use appropriate container name, container element type, scope resolution operator Data Structures Using C++ 2E

Iterators (cont’d.) typedef const_iterator Modify container elements using an iterator into a container and dereferencing operator (*) Prevents iterator from modifying elements of container declared as constant Every container contains typedef const_iterator Read-only iterator Data Structures Using C++ 2E

Iterators (cont’d.) typedef reverse_iterator Every container contains typedef reverse_iterator Used to iterate through the elements of a container in reverse Data Structures Using C++ 2E

Iterators (cont’d.) typedef const_reverse iterator Read-only iterator Used to iterate through elements of a container in reverse Required if Container declared as const Need to iterate through the elements of the container in reverse Data Structures Using C++ 2E

Summary STL Iterators Algorithms Provides class templates Process lists, stacks, and queues Three main components Containers, iterators, and algorithms STL containers: class templates Iterators Step through the elements of a container Algorithms Manipulate elements in a container Data Structures Using C++ 2E

Summary (cont’d.) Main categories of containers Sequence containers, associative containers, container adapters Three predefined sequence containers vector, deque, and list copy algorithm Copies elements in a given range to another place Five categories of iterators: input, output, forward, bidirectional, random access iterator Data Structures Using C++ 2E