Today’s Learning Objective

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

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.
Vectors, lists and queues
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
More on the STL vector list stack queue priority_queue.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
1 The Standard Template Library Drozdek Section 3.7.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Motivation for Generic Programming in C++
“Generic Programming” ECE 297
Standard Template Library
Concepts of Programming Languages
Chapter 4 Linked Structures.
Pointers and Linked Lists
Standard Template Library
C++ Templates.
Lecture 7-2 : STL Iterators
Exceptions, Templates, and the Standard Template Library (STL)
CS Data Structures Chapter 8 Lists Mehmet H Gunes
C++ Standard Library.
Standard Template Library (STL)
Chapter 4 Linked Lists.
Generic Programming Techniques in C++
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Friday, February 16, 2018 Announcements… For Today… 4.5-, pgs. 252
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
(5 - 1) Object-Oriented Programming (OOP) and C++
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
Chapter 17 Linked Lists.
Chapter 4 Inheritance.
Chapter 7 Functions of Random Variables (Optional)
Chapter 14 Graphs and Paths.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
priority_queue<T>
Constructors and Other Tools
Summary Array List.
(5 - 1) Object-Oriented Programming (OOP) and C++
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
The Facts to Be Explained
STL (Standard Template Library)
C++ Programming: chapter 10 – STL
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 2 Reference Types.
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

Today’s Learning Objective Standard Template Library Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Standard Template Library Recall array and linkedlist structures Standard Template Library (STL) Includes libraries for all such data structures Each data structure in STL contains Containers: Like data structures Iterators: Like pointers Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Containers Container classes in STL Each has own iterator Each is template class with parameter for particular data type to be stored e.g., Lists of int Each has own iterator Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Different Containers Sequential Associative 1st element, next element, … to last element Examples: vector, linked list Associative Example: set and map Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Iterators Generalization of a pointer Typically even implemented with pointer Designed to hide details of implementation Provide uniform interface across different container classes Each container class has "own" iterator type Similar to how each data type has own pointer type Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Manipulating Iterators overloaded operators: ++, --, ==, != * So if p is iterator variable, *p gives access to data pointed to by p Vector container has members begin() and end() c.begin(); //Returns iterator for 1st item in c c.end(); //Returns "test" value for end Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Cycling with Iterators Cycling ability (c is a container; p is a iterator): for (p=c.begin(); p!=c.end(); p++) { //process *p //*p is current data item } Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Vector Container: Iterator: Difference between vector and array vector<int> container; Iterator: vector<int>::iterator p; Difference between vector and array Array size is fixed, while vector size is flexible Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Example: Iterators Used with a Vector (1 of 2) #include <iostream> #include <vector> using namespace std; int main( ) { vector<int> container; for (int i = 1; i <= 4; i++) container.push_back(i); cout << "Here is what is in the container:\n"; vector<int>::iterator p; for (p = container.begin( ); p != container.end( ); p++) cout << *p << " "; cout << endl;

Example: Iterators Used with a Vector (2 of 2) cout << "Setting entries to 0:\n"; for (p = container.begin( ); p != container.end( ); p++) *p = 0; cout << "Container now contains:\n"; for (p = container.begin( ); p !=container.end( ); p++) cout << *p << " "; cout << endl; return 0; } Here is what is in the container: 1 2 3 4 Setting entries to 0: Container now contains: 0 0 0 0

Random Access of vector iterators Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

List Container: Iterator: Difference between vector and list list<int> container; Iterator: list<int>::iterator p; Difference between vector and list Vector allows random access while list do not allow Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Display 19.4 Two Kinds of Lists STL list is a double link list Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Using the list Container(1 of 2) #include <iostream> #include <list> using namespace std; int main( ) { list<int> LO; for (int i = 1; i <= 3; i++) LO.push_back(i); LO.push_front(i); } cout << "List contains:\n"; list<int>::iterator iter; for (iter = LO.begin(); iter != LO.end(); iter++) cout << *iter << " "; cout << endl;

Using the list Container(2 of 2) cout << "Setting all entries to 0:\n"; for (iter = LO.begin(); iter != LO.end(); iter++) *iter = 0; cout << "List now contains:\n"; cout << *iter << " "; cout << endl; return 0; } List contains: 3 2 1 1 2 3 Setting all entries to 0: List now contains: 0 0 0 0 0 0

Set Container Stores elements without repetition 1st insertion places element in set Each element value is its own key Capabilities: Add elements Delete elements Ask if element is in set Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Set Container example (1) #include <iostream> #include <set> using namespace std; int main() { set<char> s; set<char>::iterator p; s.insert('A'); s.insert('D'); s.insert('C'); s.insert('B'); Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Set Container example(2) cout << “The set contains:\n"; for (p=s.begin(); p!=s.end();p++) cout << *p << "\t"; cout << endl; s.erase('C'); cout << “The Set contains:\n"; { } return 1; The set contains: A B C D Removing C: The Set contains A B D Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

map Template Class Stores pairs of data <key type, value type> Examples: <SSN, Student GPA> <Course Number, Course Name> The first data (key type) is its key Capabilities: Add elements Delete elements Ask if element is in map Copyright © 2006 Pearson Addison-Wesley. All rights reserved.