Standard Template Library (STL)

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.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
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.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
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)
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.
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.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Introduction to olympic programming
Standard Template Library
Concepts of Programming Languages
Standard Template Library
Lecture 7-2 : STL Iterators
C++ Standard Library.
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
18 – Sequential Containers
(5 - 1) Object-Oriented Programming (OOP) and C++
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
Today’s Learning Objective
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Standard Template Library
Vectors the better arrays.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
14 – Sequential Containers
priority_queue<T>
Graphs: As a mathematics branch
C++ File Structure and Intro to the STL
(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.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Lecture 8-2 : STL Iterators and Algorithms
C++ File Structure and Intro to the STL
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
STL (Standard Template Library)
STL and Example.
C++ Programming: chapter 10 – STL
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
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.
Vectors the better arrays.
Standard Template Library
Presentation transcript:

Standard Template Library (STL)

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

Containers Container classes in STL Each is template class with parameter for particular data type to be stored e.g., Lists of int Each has own iterator

Different Containers Sequential 1st element, next element, … to last element Examples: vector, linked list Associative Example: set and map

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

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

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 }

Vector Container: vector<int> c; Iterator: vector<int>::iterator p; Difference between vector and array Array size is fixed, while vector size is flexible

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

Iterators Used with a Vector cout << "Setting entries to 0:\n"; for (p = c.begin( ); p != c.end( ); p++) *p = 0; cout << "Container now contains:\n"; for (p = c.begin( ); p !=c.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

List Container: list<int> container; Iterator: list<int>::iterator p; Difference between vector and list: Vector allows random access while list do not allow

Two Kinds of Lists STL list is a double link list

Using the list Container #include <iostream> #include <list> 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 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

#include <set> int main() { int num; bool found = false; set<int> s; set<int>::iterator p; for (int i=1; i<=5; i++) s.insert(i*10); // init set: 10 20 30 40 50 cout << "Set contains: " ; for (p = s.begin(); p != s.end(); p++) cout << ' ' << *p; cout << '\n'; cout << "Enter a number to look for in the set: " ; cin >> num; p = s.find(num); if (p != s.end()) cout << "Found " << (*p) << " in set S\n"; else cout << "Not found\n"; }

cout << "\nEnter a number to erase from set: " ; cin >> num; if (s.find(num)!= s.end()) { // don't erase if not in set s.erase(num); cout << "Erasing " << num << " from set .....\n"; } else cout << num << " is not in set and can't be erased\n"; cout << "Set contains: " ; for (p = s.begin(); p != s.end(); p++) cout << " " << *p; cout << "\n\n";

Set contains: 10 20 30 40 50 Enter a number to look for in the set: 20 Found 20 in set S Enter a number to erase from set: 35 35 is not in set and can't be erased : Enter a number to look for in the set: 25 Not found Enter a number to erase from set: 30 Erasing 30 from set ..... Set contains: 10 20 40 50

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