STL Common tools for C++.

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Advertisements

Data Structures Using C++ 2E
Beginning C++ Through Game Programming, Second Edition
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms.
Data Structures Using C++ 2E
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
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:
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
 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.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Standard Template Library
CS212: Object Oriented Analysis and Design
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Programming with ANSI C ++
Standard Template Library
CS 215 Final Review Ismail abumuhfouz Fall 2014.
ECE 264 Object-Oriented Software Development
Motivation and Overview
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Standard Template Library (STL)
Chapter 4 Linked Lists.
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Collections Intro What is the STL? Templates, collections, & iterators
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
Array Lists Chapter 6 Section 6.1 to 6.3
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Vectors the better arrays.
CS212: Object Oriented Analysis and Design
STL - Algorithms.
Standard Template Library Model
STL Iterators Separating Container from Data Access.
The Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
COP 3330 Object-oriented Programming in C++
Data Structures & Algorithms
COP 3330 Object-oriented Programming in C++
STL List.
Collections Intro What is the STL? Templates, collections, & iterators
Standard C++ Library Part II.
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
The List Container and Iterators
STL List.
A dictionary lookup mechanism
Presentation transcript:

STL Common tools for C++

STL Standard Template Library (STL) Templated code for common data structures, algorithms

Sequence Containers Sequence (indexed) container: every object in the container has a specific position Three predefined sequence containers: vector deque list

Vector Container class proving dynamic array Declaration includes type: #include <vector> … vector<int> intList; vector<string> stringList;

Constructors Construction options: () : empty (size) : filled with size copies of 0 or default constructor (size, value) : filled with size copies of value {list} vector<int> v; //empty vector<int> v2(10); //10 default ints vector<int> v3(10, 99); //10 copies of 99 vector<int> v4 {1,2,2}; //3 named elements

Fundamental Ops Add item to end: v.push_back(T) Get size v.size() Access element v[index] – unsafe/no bounds check v.at(index) – blow up if out of bounds

Array Replacement Most of the time should use vector not array Knows own size Much more capable Speed is the same When used correctly and compiler optimizes Unoptimized Optimized

Big Differences Assignment does copy of contents of vector Will not deep copy pointers Make sure to pass by reference to avoid copy

Iterators Iterator Pointer like object Know how to traverse collection Standard way to access elements of any collection

Iterators Every collection also defines an iterator myIterator is an iterator over a vector of chars

Iterator Position Asking container for: .begin() : iterator pointing to first element .end() : iterator pointing just after last element //get location of first element of container vector<char>::iterator myIt = container.begin(); //get location just past last element vector<char>::iterator endIt = container.end();

Reminder - Pointer Arithmetic Adding 1 to pointer moves it one element: Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 0x110 0x109 0x108 0x107 x 5 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Reminder - Pointer Arithmetic Adding 1 to pointer moves it one element: Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 0x110 0x109 0x108 0x107 x 5 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Reminder - Pointer Arithmetic Adding 1 to pointer moves it one element: Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 x 2.5 0x110 0x109 0x108 0x107 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Reminder - Pointer Arithmetic Adding 1 to pointer moves it one element: Size of element determines size of move Address Identifier Value 0x116 0x115 0x114 0x113 0x112 0x111 x 2.5 0x110 0x109 0x108 0x107 0x106 0x105 0x104 0x103 p 0x102 0x101 0x100

Iterator Movement Doing arithmetic with iterator moves it:

Iterator Access Use dereference operator to access: and -> to access members of what iterator points to:

Iterator Based Loop Start at begining

Iterator Based Loop Go up to end

Iterator Based Loop Step by one position Preincrement/decrement more efficient than post

Iterator Based Loop Print the current item

Auto C++11 supports auto type: This is a bad use…

Auto C++11 supports auto type: Good use : shorten obvious but complex types

Going Backwards Can start at (end() – 1) and -- to begin() OR use Reverse Iterator Start at rbegin() End at rend() Count FORWARD to go back

Iterators as Indexes Vector.insert(iterator, value) Uses iterator as location:

Iterators as Indexes vector.erase(iterator) Removes item at given location Advanced iterator to next item