More on the STL vector list stack queue priority_queue.

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.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
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.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Standard Containers: Vectors
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.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
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:
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
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.
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.
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.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
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.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
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.
1 The Standard Template Library Drozdek Section 3.7.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
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
Standard Template Library
Regarding homework 9 Many low grades
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Standard Template Library Model
Recursive Linked List Operations
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
C++ Programming: chapter 10 – STL
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Presentation transcript:

More on the STL vector list stack queue priority_queue

STL Container Classes Sequence containers organize items by position: 1 st, 2 nd, 3 rd, …., last –vector, list, deque Adapter containers are restricted versions of other containers –stack, queue, priority_queue Associative containers organize items based on a key value –set, map, multiset, multimap

vector vs. list vector can expand to hold as many items as needed O(1) insert/delete at end of the list O(n) insert/delete at position i in the list indexing (vec[i]) gives gives O(1) access to any position list can expand to hold as many items as needed O(1) delete of any item O(1) insert before any item no indexing – cannot access item at position i in O(1)

Use different storage structures vector items stored in an array (contiguous storage) 0 list items stored in a linked list (non-contiguous storage)

Partial list API list() //construct an empty list push_back(item) //add item at end of list pop_back() //remove item at end of list push_front(item) //add item at front of list pop_front() //remove item at front of list size() //return number of items in list back() //return item at end of list front() //return item at front of list

STL iterators an iterator is a generalization of a pointer –used to access an element in the container each STL container class has a nested iterator class associated with it vector ::iterator list ::iterator STL container classes have methods which return iterators –begin() //returns an iterator accessing first element –end() //returns an iterator that is past the last //element

iterator operations iter++ //access next element iter-- //access prior element == and != //compare 2 iterators *iter //dereference the iterator

An Example list myList; // store some items in myList list ::iterator iter; iter = myList.begin(); while (iter != myList.end()) { //process *iter iter++; }

STL Components Container ClassesAlgorithms Iterators Container classes - templates for classes which hold a collection of elements Algorithms - templates for functions which operate on a range of elements from a container –range is specified by iterators Iterators –give access to the element in a container –allow for movement from one element to another

STL Algorithms designed to operate on a sequence of elements rather than on a specific container the sequence is designated by two iterators all container classes have the following two methods –begin( ) - returns an iterator positioned at the container's first element –end( ) - returns an iterator positioned past the container's last element (past-the-end) C.begin( ), C.end( ) specifies a sequence which contains all elements of the container C

STL Algorithms some examples of STL algorithms –find(iter1, iter2, value) //returns an iterator –max_element(iter1, iter2) //returns an iterator –sort(iter1, iter2) //sorts using < –for_each(iter1, iter2, F) //applies F to every //item see STL Programmer's Guide link on home page

#include using namespace std; void set(int & val); void display(int val); int main( ) { vector A(5); for_each(A.begin( ), A.end( ), set); // would not work if vector A; used for_each(A.begin( ), A.end( ), display); cout << endl; sort(A.begin( ), A.end( )); // operator< must be defined for A's element type for_each(A.begin( ), A.end( ), display); cout << endl; return 0; } void set(int & val) { val = rand( ); } void display(int val) { cout << " " << val; } Press any key to continue