STL Standard Template Library

Similar documents
STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

STL: C++ Standard Library

PIC 10A. Lecture 23: Intro to STL containers

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

8/19/2014. Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input size

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

CS197c: Programming in C++

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Template based set of collection classes STL collection types (container types)

SSE2034: System Software Experiment 3

Computational Physics

Standard Template Library

Dynamic Data Structures

Lecture-5. STL Containers & Iterators

Standard Template Library

CS 216 Exam 1 Fall SOLUTION

Templates and Vectors

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

CSE 100: C++ TEMPLATES AND ITERATORS

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Abstract Data Types 1

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

CSE 100: C++ TEMPLATES AND ITERATORS

STL Containers. Porter Scobey Also,

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CPSC 427a: Object-Oriented Programming

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

Exceptions, Templates, and the STL

Abstract Data Types 1

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

CSI33 Data Structures

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

STL Quick Reference for CS 241

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers

The Standard Template Library. An introduction

Lesson 13 - Vectors Dynamic Data Storage

Unit 4 Basic Collections

Containers in C++ and Java

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

/ binary_search example #include <iostream> // std::cout #include <algorithm> // std::binary_search, std::sort #include <vector> // std::vector

To know the relationships among containers, iterators, and algorithms ( 22.2).

1 Short Answer (7 Points Each)

CSE 100: GRAPH ALGORITHMS

1. The term STL stands for?

COEN244: Class & function templates

Олимпиада по информатике

Lecture on pointers, references, and arrays and vectors

Chapter 5. The Standard Template Library.

Function Templates. Consider the following function:

Programming in C++ using STL. Rex Jaeschke

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Lab 8 The Stack (LIFO) Structure

The Standard Template Library Classes

ADTs: Stacks and Queues

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

7 TEMPLATES AND STL. 7.1 Function Templates

19.1 The Standard Template Library

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

List, Stack, and Queues

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

CPSC 427: Object-Oriented Programming

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

TEMPLATES AND ITERATORS

Unit 4: Stacks and Queues

Lectures 11,12. Online documentation & links

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

STL. Prof Tejada Week 14

CS 103 Unit 12 Slides

Module 9. Templates & STL

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Computer Programming

9/17/2015 7:56 AM. CSCE 625 Programing Assignment #1 due: Tues, Sep 22 (by start of class) Objective

CSE 100: C++ TEMPLATES AND ITERATORS

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

Vectors. CIS 15 : Spring 2007

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS11 Advanced C++ Fall Lecture 1

CS193D Handout 12 Winter 2005/2006 January 30, 2006 Introduction to Templates and The STL

Problem Set 1. You might want to read Chapter 12 of the course reader before attempting this problem.

Concurrent Data Structures in C++ CSInParallel Project

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

Data Structures (INE2011)

Chapter 18: Stacks And Queues

Transcription:

STL Standard Template Library September 22, 2016 CMPE 250 STL Standard Template Library September 22, 2016 1 / 25

STL Standard Template Library Collections of useful classes for common data structures Ability to store objects of any type (template) Containers form the basis for treatment of data structures Container class that stores a collection of data STL consists of 10 container classes: CMPE 250 STL Standard Template Library September 22, 2016 2 / 25

STL Containers Sequence Container Stores data by position in linear order: First element, second element, etc. All containers Use same names for common operations Have specific operations Associate Container Stores elements by key, such as name, ID number or part number Access an element by its key which may bear no relationship to the location of the element in the container Adapter Container Contains another container as its underlying storage structure CMPE 250 STL Standard Template Library September 22, 2016 3 / 25

STL Containers Sequence Container Vector Deque List Adapter Containers Stack Queue Priority queue Associative Container Set, multiset Map, multimap CMPE 250 STL Standard Template Library September 22, 2016 4 / 25

How to access Components - Iterator Iterator is an object that can access a collection of like objects one object at a time. An iterator can traverse the collection of objects. Each container class in STL has a corresponding iterator that functions appropriately for the container For example: an iterator in a vector class allows random access An iterator in a list class would not allow random access (list requires sequential access) CMPE 250 STL Standard Template Library September 22, 2016 5 / 25

Common Iterator Operations * Return the item that the iterator currently references ++ Move the iterator to the next item in the list - Move the iterator to the previous item in the list == Compare two iterators for equality!= Compare two iterators for inequality CMPE 250 STL Standard Template Library September 22, 2016 6 / 25

Vector Container Generalized array that stores a collection of elements of the same data type Vector similar to an array Vectors allow access to its elements by using an index in the range from 0 to n-1 where n is the size of the vector Vector vs array Vector has operations that allow the collection to grow and contract dynamically at the rear of the sequence CMPE 250 STL Standard Template Library September 22, 2016 7 / 25

Vector Container Example: #include <vector>... vector<int> scores (100); vector<passenger>passengerlist(20); //100 integer scores //list of 20 passengers CMPE 250 STL Standard Template Library September 22, 2016 8 / 25

Vector Container Allows direct access to the elements via an index operator Indices for the vector elements are in the range from 0 to size() -1 Example: #include <vector> vector <int> v(20); v[5]=15; CMPE 250 STL Standard Template Library September 22, 2016 9 / 25

Vector Example // constructing vectors #include <iostream> #include <vector> using namespace std; int main () { // constructors used in the same order as described above: vector<int> first; // empty vector of ints vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); cout << "The contents of fifth are:"; for (vector<int>::iterator it = fifth.begin(); it!= fifth.end(); ++it) cout << << *it; cout << \n ; } return 0; CMPE 250 STL Standard Template Library September 22, 2016 10 / 25

List Container Stores elements by position Each item in the list has both a value and a memory address (pointer) that identifies the next item in the sequence To access a specific data value in the list, one must start at the first position (front) and follow the pointers from element to element until data item is located. List is not a direct access structure Advantage: ability to add and remove items efficiently at any position in the sequence CMPE 250 STL Standard Template Library September 22, 2016 11 / 25

STL List Class Constructors and assignment list <T> v; list <T> v(alist); ll=alist; Access l.front() returns the first element in the list l.back() returns the last element in the list CMPE 250 STL Standard Template Library September 22, 2016 12 / 25

STL List Class (Cont.) Insert and Remove l.push_front(value) l.push_back(value) Iterator Delaration list<t>::iterator itr; Iterator Options itr = l.begin() set iterator to beginning of the list itr = l.end() set iterator to after the end of the list CMPE 250 STL Standard Template Library September 22, 2016 13 / 25

List Example #include <iostream> #include <list> using namespace std; // Simple example uses type int int main() { list<int> L; L.push_back(0); // Insert a new element at the end L.push_front(0); // Insert a new element at the beginning L.insert(++L.begin(),2); // Insert "2" before position of first argument // (Place before second argument) L.push_back(5); L.push_back(6); list<int>::iterator i; } for(i=l.begin(); i!= L.end(); ++i) cout << *i << " "; cout << endl; return 0; CMPE 250 STL Standard Template Library September 22, 2016 14 / 25

Stack Container Adapter Container These containers restrict how elements enter and leave a sequence Stack allows access at only one end of the sequence (top) Adds objects to container by pushing the object onto the stack Removes objects from container by popping the stack LIFO ordering (last end, first out) CMPE 250 STL Standard Template Library September 22, 2016 15 / 25

Stack Example // stack::push/pop #include <iostream> #include <stack> using namespace std; // cout // stack int main () { stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); cout << "Popping out elements..."; while (!mystack.empty()) { cout << << mystack.top(); mystack.pop(); } cout << \n ; } return 0; CMPE 250 STL Standard Template Library September 22, 2016 16 / 25

Queue Container Queue Allows access only at the front and rear of the sequence Items enter at the rear and exit from the front Example: waiting line at a grocery store FIFO ordering (first-in first-out ) push(add object to a queue) pop (remove object from queue) CMPE 250 STL Standard Template Library September 22, 2016 17 / 25

Queue Example #include <iostream> // cin, cout #include <queue> // queue using namespace std; int main () { queue<int> myqueue; int myint; cout << "Please enter some integers (enter 0 to end):\n"; do { cin >> myint; myqueue.push (myint); } while (myint); cout << "myqueue contains: "; while (!myqueue.empty()) { cout << << myqueue.front(); myqueue.pop(); } cout << \n ; } return 0; CMPE 250 STL Standard Template Library September 22, 2016 18 / 25

Priority Queue Container Priority queue Operations are similar to those of a stack or queue Elements can enter the priority queue in any order Once in the container, a delete operation removes the largest (or smallest) value CMPE 250 STL Standard Template Library September 22, 2016 19 / 25

Set Container Set Collection of unique values, called keys or set members Contains operations that allow a programmer to: determine whether an item is a member of the set insert and delete items very efficiently CMPE 250 STL Standard Template Library September 22, 2016 20 / 25

Set Example #include <iostream> #include <set> using namespace std; int main () { set<string> s; cout << "Adding Hello and World to the set twice" << endl; s.insert("hello"); s.insert("world"); s.insert("hello"); s.insert("world"); cout << "Set contains:"; while (!s.empty()) { cout << << *s.begin(); s.erase(s.begin()); } } return 0; CMPE 250 STL Standard Template Library September 22, 2016 21 / 25

Multi-Set Container A multi-set is similar to a set, but the same value can be in the set more than once Multi-set container allows duplicates CMPE 250 STL Standard Template Library September 22, 2016 22 / 25

Map Container Implements a key-value relationship Implements Programmer can use a key to access corresponding values Example: key could be a part number such as X89-21 that corresponds to a part: 912 price and MarsOto manufacturer CMPE 250 STL Standard Template Library September 22, 2016 23 / 25

Multi-map Container Similar to a map container Multi-map container allows duplicates CMPE 250 STL Standard Template Library September 22, 2016 24 / 25

Writing classes that work with the STL Classes that will be stored in STL containers should explicitly define the following: Default constructor Copy constructor Destructor operator= operator== operator< Not all of these are always necessary, but it might be easier to define them than to figure out which ones you actually need Many STL programming errors can be traced to omitting or improperly defining these methods CMPE 250 STL Standard Template Library September 22, 2016 25 / 25