C++ STL CSCI 3110.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Chapter 6 Queues and Deques.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Data Structures Using C++ 2E
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Quick Overview of STL STL = Standard Template Library Main concept : Container, Iterator Application : Linked list, Stack etc.
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++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using C++ 2E
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Containers Overview and Class Vector
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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)
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data structures Abstract data types Java classes for Data structures and ADTs.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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.
3 Data. Software And Data Data Data element – a single, meaningful unit of data. Name Social Security Number Data structure – a set of related data elements.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
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)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CSCI-383 Object-Oriented Programming & Design Lecture 30.
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
18 Chapter Stacks and Queues
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Copy Constructor / Destructors Stacks and Queues
Standard Template Library (STL)
CMSC 341 Lecture 5 Stacks, Queues
Dynamic allocation (continued)
STL List.
Standard Template Library
STL List.
Presentation transcript:

C++ STL CSCI 3110

STL – Standard Template Library Collections of useful classes for common data structures Ability to store objects of any type (template) Study of containers Containers form the basis for treatment of data structures Container – class that stores a collection of data STL consists of 10 container classes: Sequence containers Adapter containers Associative containers

STL Containers Sequence Container Associate Container Stores data by position in linear order: First element, second element , etc: Associate Container Stores elements by key, such as name, social security number or part number Access an element by its key which may bear no relationship to the location of the element in the container Adapter Container Contains another container as its underlying storage structure

STL Containers Sequence Container Adapter Containers Vector Deque List Adapter Containers Stack Queue Priority queue Associative Container Set, multiset Map, multimap

Vector Container Generalized array that stores a collection of elements of the same data type Vector – similar to an array Vectors allow access to its elements by using an index in the range from 0 to n-1 where n is the size of the vector Vector vs array Vector has operations that allow the collection to grow and contract dynamically at the rear of the sequence

Vector Container Example: #include <vector> . vector<int> scores (100); //100 integer scores vector<Passenger>passengerList(20); //list of 20 passengers

Vector Container Allows direct access to the elements via an index operator Indices for the vector elements are in the range from 0 to size() -1 Example: #include <vector> vector <int> v(20); v[5]=15;

Vector Operations See http://cs.smu.ca/~porter/csc/ref/stl/cont_vector.html For list of vector operations.

List Container Stores elements by position Each item in the list has both a value and a memory address (pointer) that identifies the next item in the sequence To access a specific data value in the list, one must start at the first position (front) and follow the pointers from element to element until data item is located. List is not a direct access structure Advantage: ability to add and remove items efficiently at any position in the sequence

STL List See http://cs.smu.ca/~porter/csc/ref/stl/cont_list.html for list of STL list operations.

Stack Container Adapter Container These containers restrict how elements enter and leave a sequence Stack allows access at only one end of the sequence (top) Adds objects to container by pushing the object onto the stack Removes objects from container by popping the stack LIFO ordering (last end, first out)

Queue Container Queue Allows access only at the front and rear of the sequence Items enter at the rear and exit from the front Example: waiting line at a grocery store FIFO ordering (first-in first-out ) push(add object to a queue) pop (remove object from queue)

Priority Queue Container Operations are similar to those of a stack or queue Elements can enter the priority queue in any order Once in the container, a delete operation removes the largest (or smallest) value Example: a filtering system that takes in elements and then releases them in priority order 8 18 13 3 15 27

Set Container Set Collection of unique values, called keys or set members Contains operations that allow a programmer to: determine whether an item is a member of the set insert and delete items very efficiently Set A Set B 1 3 27 15 Buick Ford Jeep BMW

Multi-Set Container A multi-set is similar to a set, but the same value can be in the set more than once Multi-set container allows duplicates

Map Container Implements a key-value relationship Programmer can use a key to access corresponding values Example: key could be a part number such as A24-57 that corresponds to a part: 8.75 price and Martin manufacturer A22-56 A23-57 A24-57 A24-57 8.75 Martin A22-56 12.50 Calloway A23-57 4.95 Mirage

Multi-map Container Similar to a map container Multi-map container allows duplicates

How to access Components - Iterator Iterator is an object that can access a collection of like objects one object at a time. An iterator can traverse the collection of objects. Each container class in STL has a corresponding iterator that functions appropriately for the container For example: an iterator in a vector class allows random access An iterator in a list class would not allow random access (list requires sequential access)

Common Iterator Operations * Return the item that the iterator currently references ++ Move the iterator to the next item in the list -- Move the iterator to the previous item in the list == Compare two iterators for equality != Compare two iterators for inequality

STL List Class Constructors and assignment Access list <T> v; list<T> v(aList); l=aList; Access l.front() ----returns 1st element in the list l.back()----returns the last element in the list

STL List Insert and Remove Iterator Delaration Iterator Options l.push_front(value) l.push_back(value) Iterator Delaration list<T>::iterator itr; Iterator Options itr = l.begin() set iterator to beginning of the list Itr = l.end() set iterator to after the end of the list

Writing classes that work with the STL Classes that will be stored in STL containers should explicitly define the following: Default constructor Copy constructor Destructor operator = operator== operator< Not all of these are always necessary, but it might be easier to define them than to figure out which ones you actually need Many STL programming errors can be traced to omitting or improperly defining these methods

More on STL Lists Go to: http://cs.smu.ca/~porter/csc/ref/stl/

Create a client file to read in 20 numbers into a list and print the list in reverse order.