Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.

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.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Beginning C++ Through Game Programming, Second Edition
. 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.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
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.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
Writing Your Own STL Container Ray Lischner
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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:
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
LECTURE LECTURE 17 More on the Standard Template Library 23 A guided tour with common examples of the containers.
Containers Overview and Class Vector
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
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.
Chapter 19 Standard Template Library Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
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)
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,
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.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
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 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.
A gentle introduction to the standard template library (STL) Some references:
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.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
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
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
priority_queue<T>
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
STL Библиотека стандартных шаблонов
Standard Template Library
Presentation transcript:

Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson

STL Collection of useful –abstract data types (ADT) –generic algorithms Implemented as class / function templates STL is –not a part of the C++ language itself –but a part of the C++ standard!

STL Components Six main components –Containers Sequence Associative –Iterators Constant/Mutable Forward/Bi-directional/Random Access –Adaptors Container / Interator / Function –Allocators –Generic algorithms –Function objects

Containers Sequence Containers –Arrange data-items into a linear arrangement. –Container types vector deque list (Sorted) Associative Containers –Fast insert/retrieval of data-items based on keys –Container types set multiset map multimap –"multi" containers allow multiple items with same key.

Sequence Containers Vector: –Efficient add/delete back, access/modify any element Deque: –Efficient add/delete front/back, access/modify any element List: –Efficient add/del. elements anywhere, access/mod. front/back

Common Operations on Seq. Containers Descr.MethodVDLDescr.MethodVDL E front (ref) front+++ Del all clear+++ E back (ref) back+++ Add pos insert  + Add E back push_back+++ Del pos erase  + Del E back pop_back+++<+++ Add E front push_front-++!=+++ Del E front pop_front-++==+++ E at subscript [ ]++- # E size+++

#include using namespace std; int main() { // STL Vector can use as an array (grows dynamically!) vector V1(10), V2; for ( int i=0 ; i<10; i++ ) { V1[i] = i; // NOTE: cannot use subscript unless element exists! V2.push_back(i); } for ( int i=0 ; i<10; i++ ) cout << V1[i] << " " << V2[i] << endl; // STL Deque deque D1, D2; for ( int i=0 ; i<10; i++ ) { D1[i] = i; D2.push_front(i); } for ( int i=0 ; i<10; i++ ) cout << D1[i] << " " << D2[i] << endl; // STL List (no subscripting) list L1, L2; for ( int i=0 ; i<10; i++ ) { L1.push_back(i); L2.push_front(i); } for ( int i=0 ; i<10; i++ ) { cout << L1.front() << " " << L2.back() << endl; L1.pop_front(); L2.pop_back(); }

Iterators Standard interface of how to traverse elements in a container. Allows us to write generic algorithms that work for any container! ( … almost) Iterators behave in a way like pointers –* (dereference), ++, --, ==, !=, +=, -= –Can think of as generalized pointers

Iterators and Containers The container classes have methods –begin()Returns an iterator "pointing" to first element –end()Returns an iterator pointing after last element –(also rbegin() and rend() for reverse traversal) Example: –// Displays element of list L list ::iterator il; for ( il=L.begin() ; il != L.end() ; il++ ) { cout << *il << endl; }

Iterator Kind Kind –Forward iterators ==, !=, ++, * (dereference) –Bi-directional iterators -- (additially to forward iterators) –Random access iterators [], +, -, +=, -=(additionally to bi-directional iterators) Containers –List (bi-directional), vector and deque (random-access)

Iterator Declaration Mutable (default): –list ::iterator Constant –List ::const_iterator Reverse (also mutable) –list ::reverse_iterator Constant and Reverse –list ::const_reverse_iterator

Generic Algorithm template IterType find(IterType first, IterType last, const elemType& value) { for ( ; first != last; ++first) if (value == *first) return first; return last; } vector ivec; vector ::iterator it = find(ivec.begin(), ivec.end(), 1024); if (it!=ivec.end()) { …… } // found 1024 in ivec list slist; list ::iterator iter = find(slist.begin(), slist.end(), "duck"); if (iter!=slist.end()) { ……} // found "duck“ in slist

Container Adaptors Container classes implemented on top of the existing containers (adapt their interface) –stack (default container: deque) –queue (default container: deque) –priority_queue (default container: vector) Can change underlying container class –stack > –Note, need space in between > Why?

Operations on Adapter Containers Descr.MethodSQP # of elements (E) size+++ is empty? empty+++ E at top (ref) top+-+ Add E top / back push+++ Del E top / front pop+++ E at front (ref) front-+- E at back (ref) back-+-

#include using namespace std; int main() { // STL Stack stack > S; // Changing default container (note '> >', not '>>') for ( int i=0 ; i<10; i++ ) S.push(i); for ( int i=0 ; i<10; i++ ) { cout << S.top() << " "; S.top() = 2 * S.top(); cout << S.top() << endl; S.pop(); } // STL Queue queue Q; for ( int i=0 ; i<10; i++ ) Q.push(i); for ( int i=0 ; i<10; i++ ) { cout << Q.front() << endl; Q.pop(); } // STL Priority Queue priority_queue P; for ( int i=0 ; i<10; i++ ) P.push(i); for ( int i=0 ; i<10; i++ ) { cout << P.top() << endl; P.pop(); }

Associative Containers set: –Add/Delete elements to/from set (no duplications) –Can ask of membership (is in set) map: (associative array) –Add/Delete pairs to/from map (no duplications of keys) multiset / multimap essentially the same except duplications of keys allowed. abcd a1b2c1d0

Common Operations on Associative Containers Descr.MethodSM # of elements (E) size++ is empty? empty++ Add element insert++ Delete element erase++ Find element (ref) find++ The same ==++ Subscript with key []++

#include using namespace std; int main() { // STL set set S; S.insert("hi"); S.insert("how"); S.insert("are"); S.insert("you"); for ( set ::iterator is=S.begin() ; is != S.end() ; is++ ) { cout << *is << endl; } // STL map map M; M["Hi"] = 0; M["how"] = 1; M["are"] = 2; M["you"] = 1; if ( M.find("how") != M.end() ) cout << "'how' is in map" << endl; for ( map ::iterator im=M.begin() ; im!=M.end() ; im++ ) { cout first second << endl; // note use of pair }

Summary Overview of STL –Containers –Iterators Some of the containers have –additional methods not covered here –e.g. can splice lists For complete documentation (and intro): –