The Standard Template Library. An introduction

Similar documents
use static size for this buffer

CS11 Advanced C++ Fall Lecture 1

Chapter 5. The Standard Template Library.

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

Lecture-5. STL Containers & Iterators

More on Templates. Shahram Rahatlou. Corso di Programmazione++

CS197c: Programming in C++

STL: C++ Standard Library

Unit 4 Basic Collections

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

Standard Template Library

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

COEN244: Class & function templates

TEMPLATES AND ITERATORS

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

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

STL Quick Reference for CS 241

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

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Exceptions, Templates, and the STL

Introduction to C++ Introduction to C++ 1

G52CPP C++ Programming Lecture 18

C++ Standard Template Library

Computational Physics

Teaching with the STL

Function Templates. Consider the following function:

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

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

Dynamic Data Structures

Vectors. CIS 15 : Spring 2007

Standard Template Library

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

CPSC 427a: Object-Oriented Programming

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

Standard Template Library. Outline

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

Containers in C++ and Java

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

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

Programming in C++ using STL. Rex Jaeschke

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

Lambda functions. Zoltán Porkoláb: C++11/14 1

Working with Batches of Data

Modern and Lucid C++ for Professional Programmers. Part 7 Standard Containers & Iterators. Department I - C Plus Plus

STL Standard Template Library

1. The term STL stands for?

Lecture on pointers, references, and arrays and vectors

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

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

To use various types of iterators with the STL algorithms ( ). To use Boolean functions to specify criteria for STL algorithms ( 23.8).

Module 9. Templates & STL

The Standard Template Library Classes

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

List, Stack, and Queues

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

CS242 COMPUTER PROGRAMMING

Chapter 15 - C++ As A "Better C"

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

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

Templates and Vectors

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

2

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

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

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

CS

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

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

Templates & the STL. CS 2308 :: Fall 2015 Molly O'Neil

! An exception is a condition that occurs at execution time and makes normal continuation of the program impossible.

PIC10B/1 Winter 2014 Final Exam Study Guide

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

The C++ Standard Template Library

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

TABLE OF CONTENTS...2 INTRODUCTION...3

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

CSC1322 Object-Oriented Programming Concepts

Functions. Functions in C++ Calling a function? What you should know? Function return types. Parameter Type-Checking. Defining a function

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Arrays. Week 4. Assylbek Jumagaliyev

COMP6771 Advanced C++ Programming

Exam 3 Chapters 7 & 9

Introduction to C++ Systems Programming

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

SSE2034: System Software Experiment 3

Where do we go from here?

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

Life cycle of an object construction: creating a new object. Strings: constructors & assignment another type that C and C++ don't provide

CSCI-1200 Data Structures Test 3 Practice Problem Solutions

Operating systems. Lecture 9

19.1 The Standard Template Library

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Predictive Engineering and Computational Sciences. Data Structures and Generic Programming. Roy H. Stogner. The University of Texas at Austin

Object-Oriented Programming for Scientific Computing

1. Write step by step code to delete element from existing Doubly Linked List. Suppose that all declarations are done

Improve your C++! John Richardson. Lab Study Meeting 2013

Transcription:

1 The Standard Template Library An introduction

2 Standard Template Library (STL) Objective: Reuse common code Common constructs: Generic containers and algorithms STL Part of the C++ Standard Library Developed by Alexander Stepanov and Meng Lee at HP Powerful, template-based components: Containers: template data structures Iterators: like pointers, access elements of containers Algorithms: data manipulation, searching, sorting, etc.

Common STL Containers* 3 Standard Library container class Sequence containers vector deque list Associative containers set multiset map multimap Other container stack queue Description fast insertions and deletions at back direct access to any element fast insertions and deletions at front or back direct access to any element doubly linked list, fast insertion and deletion anywhere fast lookup, no duplicates fast lookup, duplicates allowed one-to-one mapping, no duplicates, fast key-based lookup one-to-many mapping, duplicates allowed, rapid key-based lookup last-in, first-out (LIFO) first-in, first-out (FIFO)

4 Iterators Iterators are objects that point to elements in a container Iterators have overloaded operators: ++ (and --) that cause it to point to the next (previous) element in the container operator * (dereferencing operator) that returns the element pointed to by the iterator operator== and operator!= Most STL containers have their corresponding iterator class containers have functions begin() that returns an iterator to the first element, and end() that returns an iterator to one after the last element

5 Bidirectional Types of Iterators can move forward and backwards Random access Like bidirectional, but can also jump to any element const iterators Cannot modify the object pointed by Reverse iterators Advance in opposite direction (back to front of the container) istream_iterator, ostream_iterator For Input and output sequences

Iterator Types Supported Sequence containers vector: random access deque: random access list: bidirectional Associative containers (all bidirectional) set multiset map multimap Container adapters (no iterators supported) stack queue 6

Iterator Operations 7 All ++p, p++ *p p = p1 p == p1, p!= p1 Bidirectional --p, p-- Random access p + i, p += i p - i, p -= i p[i] p < p1, p <= p1 p > p1, p >= p1

8 istream_iterator std::istream_iterator<type> inp(cin); Read input from cin type a = *inp; Returns the next value from cin ++inp; Extracts a new value from cin Default ctor constructs an end-of-stream iterator

9 ostream_iterator std::ostream_iterator<type> outp(cout); To output type values to cout *outp = var; Write var to cout ++outp; Advance iterator

istream_iterator and ostream_iterator Example #include <iostream> #include <iterator> using std::cout; using std::cin; using std::endl; using std::ostream_iterator; using std::istream_iterator; 10 int main() { const int maxlen=3; int i=0; vector<int> vi(maxlen); ostream_iterator<int> outp(cout); cout<<"enter "<<maxlen<<" integers:"<<endl; istream_iterator<int> inp(cin), eos; do{ vi.at(i)=*inp; }while(++i<maxlen && inp++!= eos); } for(i=0; i< maxlen; ++i, ++outp) { *outp=vi.at(i); cout<<" "; }

Algorithms 11 STL has algorithms used generically across containers Operate on elements indirectly via iterators Often operate on sequences of elements defined by pairs of iterators Common algorithms: find for_each copy remove replace sort merge

Function Objects 12 In STL function objects replace the role of function pointers A function object is an object of a class that overloads the function cal operator (operator() ) The call to operator() resembles a function call class FunctionClass { public: void operator()(int n){...} }; //... FunctionClass func; func(5);

Algorithm Example 13 #include "student.h" typedef Student lmnt; #include <algorithm> #include <vector> using std::vector; typedef vector<lmnt> Vector; #include <deque> using std::deque; typedef deque<lmnt> Deque; #include <list> using std::list; typedef list<lmnt> List; #include <iostream> #include <iterator> using std::cout; using std::cin; using std::endl; using std::ostream_iterator;

Algorithm Example 14 // function object class class Print { public: template<class T> void operator()(const T& printable) const { cout << printable << endl; } }; int main() { const int maxlen = 3; lmnt temp; Vector container1; Deque container2; List container3(2*maxlen); // Initialize Vector for(int i=0; i < maxlen ; ++i) { cin>>temp; container1.push_back(temp); }

Algorithm Example 15 // sort Vector Vector::iterator vstart=container1.begin(); Vector::iterator vend=container1.end(); sort(vstart, vend) ; // Initialize Deque for(int i = 0; i < maxlen; i++) { cin>>temp; container2.push_front(temp); } // sort Deque Deque::iterator dstart=container2.begin(); Deque::iterator dend=container2.end(); sort(dstart, dend) ; // merge the elements of Vector and Deque // and place the results in List merge(vstart,vend,dstart,dend,container3.begin()) ;

Algorithm Example 16 cout << "\nsorted elements:" << endl ; for_each(container3.begin(), container3.end(), Print()); reverse(container3.begin(), container3.end()); cout << "\nafter reversing:" << endl ; for_each(container3.begin(),container3.end(),print()); cout<<"enter element to search: "<<endl; cin>>temp; List::iterator it=find(container3.begin(),container3.end(),temp); } ostream_iterator<lmnt> os(cout); copy(it,container3.end(),os); // output elements to cout cout<<endl;

STL and Polymorphism 17 Objects inserted into an STL container are copied according to their type. Thus, any class for which an STL container is constructed should have a copy constructor. Another consequence is that an STL container cannot be polymorphic. If we want to construct a polymorphic STL container based on some base type, it has to contain smart pointers to this type (see tutorial 12 for more details.) vector<smart_ptr<student> > basetypevector; instead of vector<student > basetypevector;