Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.

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

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
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.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
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.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
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
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
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.
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.
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.
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.
 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.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
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
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
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.
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.
Object-Oriented Programming (OOP) Lecture No. 42.
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
Standard Template Library
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
priority_queue<T>
Standard Template Library
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Presentation transcript:

Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters

§22.1 STL Basics  Standard Template Library (STL) 标准模板库 A software library included in the C++ Standard Library  Purpose To standardize commonly used components  History Originally developed by Alexander Stepanov in 1980sAlexander Stepanov Accepted as part of C++ standard in Feb

Three Components of STL  Containers (容器) A container object, such as vector, used to store a collection of data, often referred to as elements  Iterators (迭代器) Objects that facilitate traversing through the elements in a container Like built-in pointers that provide a convenient way to access and manipulate the elements in a container  Algorithms (算法) Functions to manipulate data such as sorting, searching, and comparing elements. Most of them use iterators to access the elements in the container. 3

Three Types of Containers  Sequence/sequential containers, 序列容器 Represent linear data structures Three sequence containers  vector 向量, list 表, and deque 双端队列  Associative containers, 关联容器 / 联合容器 Non-linear containers that can locate elements stored in the container quickly Four associative containers  set 集合, multiset 多重集合, map 映射, and multimap 多重映射  Container adapters ,容器适配器 Constrained versions of sequence containers stack 栈, queue 队列, and priority_queue 优先队列 4

Container Classes 5

Common Functions to All Containers 6

Common Functions to Sequence/Associative Containers 7

Simple Demo Listing 22.1 gives a simple example that demonstrates how to create a vector, list, deque, set, multiset, stack, and queue. 8 SimpleSTLDemo

§22.2 STL Iterators  Iterators are objects to facilitate traversing through the elements in a container  A iterator is like a built-in pointer that can manipulate the elements in a container 9 IteratorDemo Pointers themselves are iterators. The array pointers can be treated as iterators.

Type of Iterators  Different containers may have different types of iterators  Five types: Input (Output) iterator  For reading/writing from/to container  Moving only in forward direction Forward iterator  Combination of input/output iterator Bidirectional iterator  A forward iterator that can move backward Random access iterator  A bidirectional iterator that can jump 10

Iterator Types Supported by Containers 11

Predefined Iterators  Iterators have been defined in every containers Using “typedef”  The typedefs of iterators iterator const_iterator reverse_iterator const_reverse_iterator 12 typedef int integer; integer value = 40; ReverseIteratorDemo ConstIteratorDemo

Iterator Operators  Using overloaded operators to manipulate the an iterator Move its position Access the element Compare with other iterators 13

14

Iterator Operator Demo 15 IteratorOperatorDemo vector intVector; intVector.push_back(10); intVector.push_back(20); intVector.push_back(30); intVector.push_back(40); intVector.push_back(50); intVector.push_back(60); vector ::iterator p1 = intVector.begin(); for (; p1 != intVector.end(); p1++){ cout << *p1 << " "; } cout << endl << *(--p1) << endl; cout << *(p1 - 3) << endl; cout << p1[-3] << endl; *p1 = 1234; cout << *p1 << endl;

I/O Steam Iterators  Iterators can also be used to manipulate I/O streams input iterator and output iterator 16 InputOutputStreamIteratorDemo

§22.3 Sequence Containers  Represent linear data structures 17 Header file Strong pointsWeak pointsImpl. struct. vector Random access Inserting and deleting at the end Inserting/Deleting in the middle or front array deque Random access Inserting and deleting at the front and end Inserting/Deleting in the middle array list Inserting and deleting anywhere Random accesslinkedlist

Common Functions in Sequence Container 18

Sequence Container: vector 19 VectorDemo An insert call may invalidate previously obtained iterators!

Sequence Container: deque 20 DequeDemo An insert call may invalidate previously obtained iterators!

Sequence Container: list 21 ListDemo An insert call won’t change the previously obtained iterators!

§22.4 Associative Containers  Represent non-linear data structures Elements in an associative container are sorted according to some sorting criterion (“<” by default)  For fast storage and quick retrieval using keys 22 Header file Strong pointsWeak pointsImpl. struct. set mutlset For fast storage and quick retrieval using keys Inserting/DeletingBST map multimap For fast storage and quick retrieval using keys Inserting/DeletingBST

Common Functions in Associative Containers 23

Associative Containers: set and multiset  Mathematical set to store simple elements  set and multiset are identical except that multiset allows duplicate keys 24 SetDemo

Associative Containers: map and multimap  Storage of mapping from one data item (a key) to another (a value).  map and multimap are identical except that multimap allows duplicate keys 25 MapDemo

§22.5 Container Adapters  Containers adapted from the sequence containers For handling special cases  Programmer can choose an appropriate sequence container for a container adapter 26 Header file FeaturesImpl. struct. stack Last-In-First-Outdeque*, list, vector queue First-In-First-Outdeque*, list priority_queue Largest-In-First-Outvector*, deque *: default one

Container Adapter: stack 27 StackDemo

Container Adapter: queue 28 QueueDemo

Container Adapter: priority_queue 29 PriorityQueueDemo

A Summary  Concepts STL, Container, Iterator, Algorithm Three types of containers  Features of different containers  Syntax of declaring container objects  Main functions of containers 30