Basic C++ Sequential Container Features

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

Lists: An internal look
CSE Lecture 12 – Linked Lists …
Chapter 6 Queues and Deques.
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ă.
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.
Beginning C++ Through Game Programming, Second Edition
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
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’
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:
Data Structures Using C++ 2E
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Lecture 7 : Intro. to STL (Standard Template Library)
The Standard Template Library Container Classes Version 1.0.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
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.
Using Sequential Containers Lecture 8 Hartmut Kaiser
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.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Standard Template Library a collection of useful tools.
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.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Motivation for Generic Programming in C++
Standard Template Library
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
CS212: Object Oriented Analysis and Design
Lecture 8 : Intro. to STL (Standard Template Library)
STL List.
Chapter 3 Lists, Stacks, and Queues
STL List.
Presentation transcript:

Basic C++ Sequential Container Features Contain elements of a parameterized type Ordered (non-overlapping) ranges of elements A location can’t be within more than one container Adding and copying elements is by value A container owns the elements in it Container’s destructor destroys what it contains Provide interfaces to contained values Functions to add/remove values, e.g., push_back May provide other container-specific operations, e.g. some have operator[] for random access Obeys “principle of least surprise” (no []for list)

Containers Provide Their Own Iterators Containers declare various associated iterator types // try to use const_iterators whenever you can vector<int>::iterator i; // iterator over non-const vector<int>::const_iterator ci; // iterator over const They also give iterator accessor/factory methods for beginning of and just past the end of container range // v is previously declared as vector<int> v; auto ci = v.cbegin(); // use a const_iterator while (ci != v.cend()) { // compare to const_iterator cout << *ci << endl; // does not modify v ++ci; }

More About Container Iterators Iterators generalize different uses of pointers Most importantly, define left-inclusive intervals over the ranges of elements in a container [begin, end) Interface between algorithms and data structures Algorithm manipulates iterators, not containers An iterator’s value can represent 3 kinds of states: dereferencable (points to a valid location in a range) past the end (points just past last valid location in a range) singular (points to nothing) What’s an example of a pointer in each of these states? Can construct, compare, copy, and assign iterators so that native and library types can inter-operate

Properties of Iterator Intervals valid intervals can be traversed safely with an iterator An empty range [p,p) is valid If [first, last) is valid and non-empty, then [first+1, last) is also valid Proof: iterative induction on the interval If[first, last) is valid and position mid is reachable from first and last is reachable from mid then [first, mid) and [mid, last) are also valid If [first, mid) and [mid, last) are valid, then [first, last) is valid Proof: divide and conquer induction on the interval

The forward_list Container Implements a singly linked list of elements Elements not have to be contiguous in memory No random access, can only iterate forward Can only grow at front of forward_list Insertion at front is constant time, but many other operations are linear in number of elements Insertion into the middle of the list is constant time, but finding a position within the list is linear time

The vector Container Implements a dynamically sized array of elements Elements are (almost certainly) contiguous in memory Random access and can iterate forward or backward Can only grow at back of vector (reallocates as needed) Many operations are constant time, such as whether or not the container is empty, how many elements it currently holds, etc. Pushing at the back is “amortized” constant time (occasional reallocations averaged over all pushes)

The list Container Implements a doubly linked list of elements Elements do not have to be contiguous in memory No random access, but can iterate forward or backward Can grow at front or back of list

The deque Container Implements a double-ended queue of elements Elements do not have to be contiguous in memory But must be accessible in constant time (random access) Still can iterate forward or backward Still can grow at front or back of deque

Rules of Thumb for Sequential Containers Use a vector most of the time (if you can) Often balances simplicity, efficiency, flexibility well But, use a list (or maybe a forward_list) instead if you need to insert in the middle of a container Or use a deque to insert at both ends Watch out for when iterators may be invalidated Use specialized containers only when needed E.g., an array if you need a fixed sized container E.g., a string if you’re only working with characters E.g., a stack or queue container adapter if you want to restrict the container interface that can be used

An Advanced Topic: Emplacement Insertion may involve extra overhead for copying vector<string> courses; courses.push_back (“cse332”); // 2 constructor calls If the compiler and STL library you are using supports emplacement, it may be more efficient (if that matters) courses.emplace_back (“cse332”); // 1 constructor call // due to forwarding