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

Similar documents
STL: C++ Standard Library

PIC 10A. Lecture 23: Intro to STL containers

STL Standard Template Library

Chapter 5. The Standard Template Library.

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

use static size for this buffer

SSE2034: System Software Experiment 3

COEN244: Class & function templates

Standard Template Library

1. The term STL stands for?

CS197c: Programming in C++

Templates and Vectors

CS11 Advanced C++ Fall Lecture 1

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

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

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

CPSC 427a: Object-Oriented Programming

Lecture-5. STL Containers & Iterators

Computational Physics

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

Programming in C++ using STL. Rex Jaeschke

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

G52CPP C++ Programming Lecture 18

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

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

Dynamic Data Structures

VI. Templates A. Introduction.

Module 9. Templates & STL

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

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS

STL in Action: Helper Algorithms

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

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

Topics. bool and string types input/output library functions comments memory allocation templates classes

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

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

The Standard Template Library. An introduction

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

CS11 Advanced C++ Spring 2018 Lecture 2

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

Advanced C++ STL. Tony Wong

CS302. Today s Topics: More on constructor/destructor functions More on STL <list>

Containers in C++ and Java

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.

pointers & references

CSI33 Data Structures

19.1 The Standard Template Library

CSCE 110 PROGRAMMING FUNDAMENTALS

Unit 4 Basic Collections

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

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

General Advise: Don t reinvent the wheel

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

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

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

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

Introducing C++ to Java Programmers

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Sequential Containers. Ali Malik

List, Stack, and Queues

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Outline. Function calls and results Returning objects by value. return value optimization (RVO) Call by reference or by value?

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

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

Exceptions, Templates, and the STL

Linked List using a Sentinel

Working with Batches of Data

Writing Generic Functions. Lecture 20 Hartmut Kaiser hkaiser/fall_2013/csc1254.html

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

Introduction to C++ Introduction to C++ 1

Faculty of Information and Communication Technologies

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

CSC 222: Computer Programming II. Spring 2004

Bruce Merry. IOI Training Dec 2013

Why Is Repetition Needed?

Due Date: See Blackboard

W3101: Programming Languages C++ Ramana Isukapalli

I source_beg, source_end; // defines a range of values in a source container // defines the beginning of a range in a destination container

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

Computer Science II Lecture 2 Strings, Vectors and Recursion

Exercise 6.2 A generic container class

COMP322 - Introduction to C++

Sequential Containers. Ali Malik

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

Vectors. CIS 15 : Spring 2007

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

AN OVERVIEW OF C++ 1

Exam 3 Chapters 7 & 9

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Introduction to Programming using C++

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Transcription:

STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed in early 90's at Hewlett Packard Laboratories Alex Stepanov and Meng Lee became part of C++ standard in 1994 implementations available by late 90's CSC 330 OO Software Design 1 CSC 330 OO Software Design 2 Main Ideas STL components General purpose: generic data structures & algorithms, templates Flexibility: Allows for many combinations of algorithm-container Simple & uniform interface: interface through templates (not inheritence) Efficiency Containers templates for classes which hold a collection of elements Algorithms templates for functions which operate on a range of elements from a container range is specified by iterators Iterators give access to the elements in a container allow for movement from one element to another Container classes Iterators Algorithms CSC 330 OO Software Design 3 CSC 330 OO Software Design 4 Components Containers: data structures vector, list, map, set, deque Adaptors: high-level data structure stack, queue, priority_queue Iterators: allow access into containers Algorithms: base algorithms sort, copy, search, min, max, Streams: input/output String Containers CSC 330 OO Software Design 5 CSC 330 OO Software Design 6 1

Basic Sequential Containers vector<t> growable, self-contained, type-independent array element type specified when a vector is declared vector<double> numbers; vector<cashier> checkoutstations; has a set of operations (methods) capacity increases when needed some vectors vector<int> v1; (capacity is 0) vector<float> v2 (10); (capacity is 10; size is 10) vector<string> v3 (5, "C++"); (capacity is 5; size is 5) CSC 330 OO Software Design 7 CSC 330 OO Software Design 8 Some vector<t> methods V.capacity( ) //size of array currently allocated V.size( ) //number of values V contains V.empty( ) //true iff V.size( ) is 0 V.reserve(n) //grow V so its capacity is n V.push_back(val) //add val at end of V V.pop_back( ) //erase V's last element V[i] //access element of V whose index is i V.at(i) //access element of V whose index is i CSC 330 OO Software Design 9 #include <iostream> #include <vector> using namespace std; bool Search(const vector<int> & V, int item); int main ( ) { vector<int> numbers; int number; while (cin >> number) { if (Search(numbers, number)) cout << "Duplicate" << endl; else numbers.push_back(number); // enter <control> D to stop the loop cout << "number of unique values: " << numbers.size( ); return 0; bool Search(const vector<int> & V, int item) { int p = 0; while(p < V.size( ) ) if (item = = V[p]) // or V.at(p) return true; else p++; return false; CSC 330 OO Software Design 10 list<t> class a list<t> data structure another STL container class used for storing a linear collection of like items comparison to a vector? linked list vs array is the underlying data structure no indexing (iterators are bidirectional) inserts and deletes anywhere are done in a constant amount of time head size 2 ------ CSC 330 OO Software Design 11 CSC 330 OO Software Design 12 2

Basic list class methods Some more list methods list( ); // construct an empty list list (const list<t> & alist); // copy constructor ~list( ); // destructor list<t> operator= (const list<t> & alist); // assignment operator bool empty( ); int size( ); L.push_back(value) // append value to L L.push_front(value) // insert value at front of L L.insert(pos, value) // insert value into L at // position indicated by iterator pos L.front( ) // return L's first element L.back( ) // return L's last element L.begin( ) // return an iterator positioned at start L.end( ) // return the"past the end" iterator L.sort( ) // sort L's elements using < CSC 330 OO Software Design 13 Why not sort (L.begin( ), L.end( ));? CSC 330 OO Software Design 14 #include <list> 9 #include <iostream> 7 using namespace std; 5 int main ( ) { 3 list<int>l; 10 L.push_back (9); 8 L.push_back (7); 6 L.push_back (5); 4 L.push_back (3); Press any key to continue list<int>::iterator p; for (p = L.begin ( ); p!= L.end ( ); p++) cout << *p << endl; for (p = L.begin ( ); p!= L.end ( ); p++) (*p)++; for (p = L.begin ( ); p!= L.end ( ); p++) cout << *p << endl; return 0; CSC 330 OO Software Design 15 Associative Containers CSC 330 OO Software Design 16 Basic Associative Containers Instantiating STL Container CSC 330 OO Software Design 17 CSC 330 OO Software Design 18 3

Iterators Member Functions CSC 330 OO Software Design 19 CSC 330 OO Software Design 20 Member Functions of Sequential Container Examples CSC 330 OO Software Design 21 CSC 330 OO Software Design 22 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main ( ) { vector<int> numbers; int number; while (cin >> number) { if (find (numbers.begin ( ), numbers.end ( ), number)!= numbers.end ( )) cout << "Duplicate" << endl; else numbers.push_back(number); cout << "number of unique values: " << numbers.size( ); return 0; CSC 330 OO Software Design 23 #include <iostream> #include <vector> #include <algorithm> #include <cstdlib> using namespace std; void set (int & val); void display (int val); int main( ) { vector<int> A(5); for_each (A.begin ( ), A.end ( ), set); // would not work if vector<int> A; used for_each (A.begin ( ), A.end ( ), display); cout << endl; sort (A.begin ( ), A.end ( )); // operator< must be defined for A's element type for_each (A.begin ( ), A.end ( ), display); cout << endl; return 0; void set (int & val) { val = rand ( ); void display (int val) { cout << " " << val; 41 18467 6334 26500 19169 41 6334 18467 19169 26500 Press any key to continue CSC 330 OO Software Design 24 4

STL Iterators STL iterators Iterators are allow to traverse sequences Methods operator* operator++, and operator Different types of iterators - to support read, write and random access Containers define their own iterator types Changing the container can invalidate the iterator iterators are "pointer-like" objects provide a generic way to access the elements of any container class many STL algorithms require iterators as arguments some STL algorithms return an iterator each STL container class has an iterator class associated with it vector<t>::iterator list<t>::iterator stack<t> and queue<t> don't have iterators why? CSC 330 OO Software Design 25 CSC 330 OO Software Design 26 Iterators Iterator categories category determines available operations forward iterator iter++ (increment) *iter (dereference) == and!= (equality comparison) bidirectional iterator adds iter-- (decrement) random-access iterator adds iter[n] (constant time access to arbitrary element) iter =+ n (increment n times) CSC 330 OO Software Design 27 CSC 330 OO Software Design 28 Iterator Types Output Input Forward Bi-directional Random Read x = *i x = *i x = *i x = *i Write *i = x *i = x *i = x *i = x Iteration ++ ++ ++ ++, -- ++, --, +, -, +=, -= Comparison ==,!= ==,!= ==,!= ==,!=, <, >, <=, >= Output: write only and can write only once Input: read many times each item Forward supports both read and write Bi-directional support also decrement Random supports random access (just like C pointer) Iterators & Containers Bidirectional iterators: list, map, set Random access iterators: vector, deque Input/output/forward iterators: iostreams CSC 330 OO Software Design 29 CSC 330 OO Software Design 30 5

Iterators and Containers Data Access T::iterator iterator type for type T begin() front of the container end() element after last Container C Container::iterator i for( i = C.begin(); i!= C.end(); i++) // do something with *i CSC 330 OO Software Design 31 CSC 330 OO Software Design 32 Data Insertion Constant Iterators CSC 330 OO Software Design 33 CSC 330 OO Software Design 34 Example STL Algorithms some examples of STL algorithms find (iter1, iter2, value) //returns an iterator max_element (iter1, iter2) //returns an iterator sort (iter1, iter2) //sorts using < for_each (iter1, iter2, F) //applies F to every //item see STL Programmer's Guide link on home page CSC 330 OO Software Design 35 CSC 330 OO Software Design 36 6

Algorithms STL Algorithms are function templates designed to operate on a sequence of elements rather than methods the sequence is designated by two iterators most container classes have the following two methods begin( ) - returns an iterator positioned at the container's first element end( ) - returns an iterator positioned past the container's last element (past-theend) C.begin( ), C.end( ) specifies a sequence which contains all elements of the container C CSC 330 OO Software Design 37 CSC 330 OO Software Design 38 Algorithms Most STL algorithms works on sequences Sequences are passed as two iterators: beginning element element one after last p q Algorithms cont d sequence [p,q) Algorithms depend on iterator type not on container type CSC 330 OO Software Design 39 CSC 330 OO Software Design 40 Algorithms cont d Using sort( ) CSC 330 OO Software Design 41 CSC 330 OO Software Design 42 7

Using sort( ) cont d Function Objects CSC 330 OO Software Design 43 CSC 330 OO Software Design 44 Iterators and Algorithms More Iterators CSC 330 OO Software Design 45 CSC 330 OO Software Design 46 Discussions Plugging Iterators CSC 330 OO Software Design 47 CSC 330 OO Software Design 48 8

Discussions for_each( ( ) algorithm CSC 330 OO Software Design 49 CSC 330 OO Software Design 50 Discussions Associative Containers set class CSC 330 OO Software Design 51 CSC 330 OO Software Design 52 map class Container Adaptor CSC 330 OO Software Design 53 CSC 330 OO Software Design 54 9