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

Size: px
Start display at page:

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

Transcription

1 Lecture 21 Standard Template Library STL: At a C++ standards meeting in 1994, the committee voted to adopt a proposal by Alex Stepanov of Hewlett-Packard Laboratories to include, as part of the standard C++ library, a collection of generic algorithms that he, Meng Lee and their colleagues had developed. Collectively, the classes and algorithms in this library are known as the Standard Template Library (STL). The idea of STL is to make various algorithms as independent as possible of the data structures on which they act. A simple, but very limited, view of STL is the generality that using template functions provides. For example: template< class T> void sort( T rgtobjects[], int nsize) This function, presumably, would sort any type of array of nsize objects (of type T). But STL is much more. Consider the following task. Suppose we wanted a sort algorithm that would work efficiently on either an array or a doubly linked list. STL has three major components designed to work with each other. Containers hold your objects. Iterators let you move through, change, retrieve, etc. your objects in a container. Algorithms implement useful tasks (like sorting) with containers and iterators. containers - containers are template classes. The are sometimes called collections. A container is an object that holds other objects. There are three types: sequence container classes, associative container classes and container adapters. First-class containers (see below) are sequence and associative containers. Sequence container classes are like arrays - they act like a linear list or array of values. Examples are: vector list like C array linked list of values

2 deque (doubly ended queues) Associative containers contain a set of objects that can be accessed through keys. Examples are: set multiset map multimap like mathematical set no duplicates set with duplicates 1:1 associations 1:N associations Container adapters modify underlying containers like vectors, lists, etc.. Examples are: stack queue priority_queue lifo fifo There are a number of standard container member functions. Here are some: size empty max_size swap begin end rbegin rend clear erase gives number of objects in container returns true if no objects in container gives max size of container swaps the elements of two containers returns iterator of first element (const and non- const version) returns iterator that points past end (const and non- const version) returns iterator that points befor beginning (const and non- const version) gives number of objects in container (const and non- const version) empties container removes object from container iterators - iterators (as we have seen) are generalizations of pointers. The are typically used to move through a sequence of items using ++, to dereference (*) and other operator overloads. The deal with first-class containers. Here are the supported iterators (notice the hierarchy): istream_iterator ostream_iterator input_iterator output_iterator to treat istream as a container to treat ostream as a container reads from a container writes to a container

3 forward_iterator random_access_iterator both input and output in one direction like forward in both directions jump around Support for the containers is as follows: vector list deque set multiset map multimap stack queue priority_queue random_access_iterator random_access_iterator none none none Iterator operators are: All types ++p p++ pre-increment iterator post-increment operator Input iterators: *p dereference as r-value p = p1 assign one iterator to another p == p1 compare two iterators p!= p1 Output iterators: *p dereference as l-value p = p1 assign one iterator to another Forward iterators have both Input and Output iterator behavior Bidirectional iterators: --p p-- pre-decrement iterator post- decrement operator Random-access iterators:

4 p+= k increment p by k positionsrator p -= k p + k iterator pointing k positions beyond p p k p[ k ] iterator pointing to kth object after p p < p1 true if iterator p < p1 p <= p1 p > p1 p >= p1 Here s some typical code one might see: #include <list> #include "str.h" using namespace std; // need this to make a list // our string class // include the right globals for STL void main() list<str> ListOfStrings; ListOfStrings.push_front( Str("Gary Koehler") ); ListOfStrings.push_front( Str("Mark Crandall") ); ListOfStrings.push_front( Str("Dan Conway") ); ListOfStrings.push_front( Str("Kenny Cheng") ); ListOfStrings.push_front( Str("Indranil Bose") ); list<str>::iterator p; // iterator p = ListOfStrings.begin(); // iterator initialized do cout << *p << endl; while ( ++p!= ListOfStrings.end() ); Output is: Indranil Bose Kenny Cheng Dan Conway Mark Crandall Gary Koehler algorithms - the stl algorithms are template functions that perform a variety of tasks like sorting or searching. Here are some examples:

5 Mutating-sequence algoithms (these modify a container): copy remove reverse_copy copy_backward remove_copy rotate fill remove_copy_if rotate_copy fill_n remove_if stable_partition generate replace swap generate_n replace_copy swap_ranges iter_swap replace_copy_if transform partition replace_if unique random_shuffle reverse unique_copy inplace_merge merge Non mutating-sequence algoithms: adjacent_find count count_if find find_if for_each equal mismatch search search_n Numerical algorithms: Other: accumulate adjacent_difference inner_product partial_sum accumulate sort stable_sort partial_sort partial_sort_copy nth_element lower_bound upper_bound equal_range binary_search There are a number of typedefs that are provided for ease of use. reference a reference to the type of element in the container

6 const_reference const reference value_type type of object in the container iterator an iterator that points to type of object in container const_iterator a const iterator reverse_iterator iterator that iterates in reverse const_reverse_iterator const version Here are STL header files for STL implemented in Visual C++ (notice no.h): <algorithm> -- for defining numerous templates that implement useful algorithms <deque> -- for defining a template class that implements a deque container <functional> -- for defining several templates that help construct predicates for the templates defined in <algorithm> and <numeric> <iterator> -- for defining several templates that help define and manipulate iterators <list> -- for defining a template class that implements a list container <map> -- for defining template classes that implement associative containers <memory> -- for defining several templates that allocate and free storage for various container classes <numeric> -- for defining several templates that implement useful numeric functions <queue> -- for defining a template class that implements a queue container <set> -- for defining template classes that implement associative containers with unique elements <stack> -- for defining a template class that implements a stack container <utility> -- for defining several templates of general utility <vector> -- for defining a template class that implements a vector container Namespaces (a minor detail): A namespace defines a set of variables and functions as contained within the same scope and gives the set a name. These are used to keep conflicts between global identifiers, possibly from different vendors or different programmers. The variables and functions are called members of the namespace. Example: namespace Boat int nlength; int nmodel; char* pszname; int NLenBoat();

7 int Boat::NlenBoat() return nlength; Boat::nLength = 15; Namespaces can be augmented anywhere in the code as: namespace Boat int nwidth; They can also be nested as in: namespace Boat namespace size int nwidth; int nlength; int nmodel; char* pszname; Boat::size::nWidth = 8; By default, the global namespace contains all external variables. It has no name and references are by the :: operator. Namespaces can also be aliased (have a duplicate name) such as: namespace Ship = Boat; A using declaration specifies that a namespace variable/function can be used without the :: qualifier as in: using Boat::nModel; nmodel = 9; A using directive specifies that all of the namespace can be referenced without the qualifier. This is done as: using namespace Boat;

8 Practical Issue: When you use a container class on an object you create, you need to provide a certain level of operability. Always think about: Copy constructor Assignment operator Many algorithms need to compare objects, so you might need: operator== operator!= operator> operator< Example Usage - a deque: Lets use a deque to hold your Rational objects. The declaration of such a beast is: deque< Rational > deqrata; Here s a complete program: #include <deque> #include <algorithm> #include "rational.h" using namespace std; // need this to make a deque // and this to do things with the deque // gotta include the right globals for STL void main() deque< Rational > deqrata; // our deque of rationals Rational a(-3, 5), b, c(2, -3), d(-4, -9); // add to deque deqrata.push_front( a ); deqrata.push_front( b ); deqrata.push_back( c ); deqrata.push_front( d ); for(int i=0;i<deqrata.size();i++)

9 cout << deqrata[i] << endl; Output is: 4/9 0/1-3/5-2/3 Same Example Usage - a deque but with an iterator: #include <deque> #include <algorithm> #include "rational.h" using namespace std; // need this to make a deque // and this to do things with the deque // gotta include the right globals for STL void main() deque< Rational > deqrata; // our deque of rationals Rational a(-3, 5), b, c(2, -3), d(-4, -9); // add to deque deqrata.push_front( a ); deqrata.push_front( b ); deqrata.push_back( c ); deqrata.push_front( d ); deque< Rational>::iterator k = deqrata.begin(); while ( k!= deqrata.end()) cout << *k++ << endl; Example Usage - a queue: The sample shows queue implementation using list and deque containers. Don t forget, queues are container adapters they need the underlying container. #include <list> #include <iostream> #include <queue>

10 #include <deque> using namespace std ; // has all the stl declarations // Using queue with list typedef list<int > INTLIST; typedef queue<int> INTQUEUE; // Using queue with deque typedef deque<char*> CHARDEQUE; typedef queue<char*> CHARQUEUE; void main(void) int size_q; INTQUEUE q; CHARQUEUE p; // Insert items in the queue(uses list) q.push(42); q.push(100); q.push(49); q.push(201); // Output the size of queue size_q = q.size(); cout << "size of q is:" << size_q << endl; // Output items in queue using front() // and use pop() to get to next item until // queue is empty while (!q.empty()) cout << q.front() << endl; q.pop(); // Insert items in the queue(uses deque) p.push("cat"); p.push("ape"); p.push("dog"); p.push("mouse"); p.push("horse"); // Output the item inserted last using back() cout << p.back() << endl;

11 // Output the size of queue size_q = p.size(); cout << "size of p is:" << size_q << endl; // Output items in queue using front() // and use pop() to get to next item until // queue is empty while (!p.empty()) cout << p.front() << endl; p.pop(); Program Output: size of q is: horse size of p is:5 cat ape dog mouse horse

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

To use various types of iterators with the STL algorithms ( ). To use Boolean functions to specify criteria for STL algorithms ( 23.8). CHAPTER 23 STL Algorithms Objectives To use various types of iterators with the STL algorithms ( 23.1 23.20). To discover the four types of STL algorithms: nonmodifying algorithms, modifying algorithms,

More information

Major Language Changes, pt. 1

Major Language Changes, pt. 1 C++0x What is C++0x? Updated version of C++ language. Addresses unresolved problems in C++03. Almost completely backwards compatible. Greatly increases expressiveness (and complexity!) of language. Greatly

More information

Unit 4 Basic Collections

Unit 4 Basic Collections Unit 4 Basic Collections General Concepts Templates Exceptions Iterators Collection (or Container) Classes Vectors (or Arrays) Sets Lists Maps or Tables C++ Standard Template Library (STL Overview A program

More information

The Standard Template Library. An introduction

The Standard Template Library. An introduction 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

More information

C++ Standard Template Library

C++ Standard Template Library C++ Standard Template Library CS 247: Software Engineering Principles Generic Algorithms A collection of useful, typesafe, generic (i.e., type-parameterized) containers that - know (almost) nothing about

More information

Boost.Compute. A C++ library for GPU computing. Kyle Lutz

Boost.Compute. A C++ library for GPU computing. Kyle Lutz Boost.Compute A C++ library for GPU computing Kyle Lutz GPUs (NVIDIA, AMD, Intel) Multi-core CPUs (Intel, AMD) STL for Parallel Devices Accelerators (Xeon Phi, Adapteva Epiphany) FPGAs (Altera, Xilinx)

More information

Lecture-5. STL Containers & Iterators

Lecture-5. STL Containers & Iterators Lecture-5 STL Containers & Iterators Containers as a form of Aggregation Fixed aggregation An object is composed of a fixed set of component objects Variable aggregation An object is composed of a variable

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

Programming with Haiku

Programming with Haiku Programming with Haiku Lesson 2 Written by DarkWyrm All material 2010 DarkWyrm In our first lesson, we learned about how to generalize type handling using templates and some of the incredibly flexible

More information

CS11 Advanced C++ Fall Lecture 1

CS11 Advanced C++ Fall Lecture 1 CS11 Advanced C++ Fall 2006-2007 Lecture 1 Welcome! ~8 lectures Slides are posted on CS11 website http://www.cs.caltech.edu/courses/cs11 ~6 lab assignments More involved labs 2-3 week project at end CS

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

Deitel Series Page How To Program Series

Deitel Series Page How To Program Series Deitel Series Page How To Program Series Android How to Program C How to Program, 7/E C++ How to Program, 9/E C++ How to Program, Late Objects Version, 7/E Java How to Program, 9/E Java How to Program,

More information

Contents. 2 Introduction to C++ Programming,

Contents. 2 Introduction to C++ Programming, cppfp2_toc.fm Page vii Thursday, February 14, 2013 9:33 AM Chapter 24 and Appendices F K are PDF documents posted online at www.informit.com/title/9780133439854 Preface xix 1 Introduction 1 1.1 Introduction

More information

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4 Steven J. Zeil July 19, 2013 Contents 1 Overview of Sets and Maps 4 1 2 The Set ADT 6 2.1 The template header................................. 14 2.2 Internal type names.................................

More information

Working Draft, Technical Specification for C++ Extensions for Parallelism, Revision 1

Working Draft, Technical Specification for C++ Extensions for Parallelism, Revision 1 Document Number: N3960 Date: 2014-02-28 Reply to: Jared Hoberock NVIDIA Corporation jhoberock@nvidia.com Working Draft, Technical Specification for C++ Extensions for Parallelism, Revision 1 Note: this

More information

C and C++ Courses. C Language

C and C++ Courses. C Language C Language The "C" Language is currently one of the most widely used programming languages. Designed as a tool for creating operating systems (with its help the first Unix systems were constructed) it

More information

New Iterator Concepts

New Iterator Concepts New Iterator Concepts Author: David Abrahams, Jeremy Siek, Thomas Witt Contact: dave@boost-consulting.com, jsiek@osl.iu.edu, witt@styleadvisor.com Organization: Boost Consulting, Indiana University Open

More information

C++ How To Program 10 th Edition. Table of Contents

C++ How To Program 10 th Edition. Table of Contents C++ How To Program 10 th Edition Table of Contents Preface xxiii Before You Begin xxxix 1 Introduction to Computers and C++ 1 1.1 Introduction 1.2 Computers and the Internet in Industry and Research 1.3

More information

Data_Structures - Hackveda

Data_Structures - Hackveda Data_Structures - Hackveda ( Talk to Live Mentor) Become a Data Structure and Algorithm Professional - (Beginner - Advanced) Skill level: Beginner - Advanced Training fee: INR 15999 only (Topics covered:

More information

Standard Library Reference

Standard Library Reference Standard Library Reference This reference shows the most useful classes and functions in the standard library. Note that the syntax [start, end) refers to a half-open iterator range from start to end,

More information

Parallelism and Concurrency in C++17 and C++20. Rainer Grimm Training, Coaching and, Technology Consulting

Parallelism and Concurrency in C++17 and C++20. Rainer Grimm Training, Coaching and, Technology Consulting Parallelism and Concurrency in C++17 and C++20 Rainer Grimm Training, Coaching and, Technology Consulting www.grimm-jaud.de Multithreading and Parallelism in C++ Multithreading in C++17 Parallel STL The

More information

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

To know the relationships among containers, iterators, and algorithms ( 22.2). CHAPTER 22 STL Containers Objectives To know the relationships among containers, iterators, and algorithms ( 22.2). To distinguish sequence containers, associative containers, and container adapters (

More information

Chapters and Appendices F J are PDF documents posted online at the book s Companion Website, which is accessible from.

Chapters and Appendices F J are PDF documents posted online at the book s Companion Website, which is accessible from. Contents Chapters 23 26 and Appendices F J are PDF documents posted online at the book s Companion Website, which is accessible from http://www.pearsonhighered.com/deitel See the inside front cover for

More information

Chapter 5. The Standard Template Library.

Chapter 5. The Standard Template Library. Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library. 5.1. Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov,

More information

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

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms 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

More information

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

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University (8 1) Container Classes & Class Templates D & D Chapter 18 Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University Key Concepts Class and block scope Access and utility functions

More information

Programming in C++ using STL. Rex Jaeschke

Programming in C++ using STL. Rex Jaeschke Programming in C++ using STL Rex Jaeschke Programming in C++ using STL 1997, 1999, 2002, 2007, 2009 Rex Jaeschke. All rights reserved. Edition: 2.0 All rights reserved. No part of this publication may

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

CS11 Advanced C++ Spring 2018 Lecture 2

CS11 Advanced C++ Spring 2018 Lecture 2 CS11 Advanced C++ Spring 2018 Lecture 2 Lab 2: Completing the Vector Last week, got the basic functionality of our Vector template working It is still missing some critical functionality Iterators are

More information

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

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2 TEMPLATES Today This lecture looks at techniques for generic programming: Generic pointers Templates The standard template library Thebestreferenceis: http://www.cppreference.com/index.html andyoucanalsoconsultchapters6amd7inthetextbook.

More information

Standard Template Library

Standard Template Library Standard Template Library Wednesday, October 10, 2007 10:09 AM 9.3 "Quick Peek" STL history 1990s Alex Stepanov & Meng Lee of HP Labs 1994 ANSI/IS0 standard Components Container class templates Iterators

More information

Distributed Real-Time Control Systems. Lecture 16 C++ Libraries Templates

Distributed Real-Time Control Systems. Lecture 16 C++ Libraries Templates Distributed Real-Time Control Systems Lecture 16 C++ Libraries Templates 1 C++ Libraries One of the greatest advantages of C++ is to facilitate code reuse. If code is well organized and documented into

More information

CS197c: Programming in C++

CS197c: Programming in C++ CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html Administration HW1 will be up this afternoon Written assignment Due in class next week See website

More information

The Standard Template Library Classes

The Standard Template Library Classes The Standard Template Library Classes Lecture 33 Sections 9.7, 9.8 Robb T. Koether Hampden-Sydney College Wed, Apr 23, 2014 Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes

More information

Parallelism in C++ J. Daniel Garcia. Universidad Carlos III de Madrid. November 23, 2018

Parallelism in C++ J. Daniel Garcia. Universidad Carlos III de Madrid. November 23, 2018 J. Daniel Garcia Universidad Carlos III de Madrid November 23, 2018 cbea J. Daniel Garcia ARCOS@UC3M (josedaniel.garcia@uc3m.es) 1/58 Introduction to generic programming 1 Introduction to generic programming

More information

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 17 November 1, 2011 CPSC 427a, Lecture 17 1/21 CPSC 427a, Lecture 17 2/21 CPSC 427a, Lecture 17 3/21 A bit of history C++ standardization.

More information

Programming Languages Technical Specification for C++ Extensions for Parallelism

Programming Languages Technical Specification for C++ Extensions for Parallelism ISO 05 All rights reserved ISO/IEC JTC SC WG N4409 Date: 05-04-0 ISO/IEC DTS 9570 ISO/IEC JTC SC Secretariat: ANSI Programming Languages Technical Specification for C++ Extensions for Parallelism Warning

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 An important part of every container is the type of iterator it supports. This determines which algorithms can be applied to the container. A vector supports random-access iterators i.e., all iterator

More information

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

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions 1. You are given a map that associates strings with lists of strings. The definition is: map words; Write

More information

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

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland May 6, 2010 ENGI

More information

Lectures 11,12. Online documentation & links

Lectures 11,12. Online documentation & links Lectures 11,12 1. Quicksort algorithm 2. Mergesort algorithm 3. Big O notation 4. Estimating computational efficiency of binary search, quicksort and mergesort algorithms 5. Basic Data Structures: Arrays

More information

Bring Back the Obvious Definition of count()

Bring Back the Obvious Definition of count() Bring Back the Obvious Definition of count() Bjarne Stroustrup AT&T Bell Laboratories Murray Hill, New Jersey 07974 Alex Stepanov, Matt Austern Silicon Graphics Inc. ABSTRACT #X3J16/96-0029 WG21/N0847

More information

use static size for this buffer

use static size for this buffer Software Design (C++) 4. Templates and standard library (STL) Juha Vihavainen University of Helsinki Overview Introduction to templates (generics) std::vector again templates: specialization by code generation

More information

Teaching with the STL

Teaching with the STL Teaching with the STL Joseph Bergin Pace University Michael Berman Rowan College of New Jersey 1 Part 1 Introduction to STL Concepts 2 STL: What and Why Generic data structures (containers) and algorithms

More information

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

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible

More information

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

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. Lectures 19, 20, 21 1. STL library examples of applications Explanations: The member function pop_back removes the last element of the controlled sequence. The member function pop_front removes the first

More information

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

Templates & the STL. CS 2308 :: Fall 2015 Molly O'Neil Templates & the STL CS 2308 :: Fall 2015 Molly O'Neil Function Templates Let's say we have a program that repeatedly needs to find the maximum value in an array In one place in our code, we need the max

More information

Scientific programming (in C++)

Scientific programming (in C++) Scientific programming (in C++) F. Giacomini INFN-CNAF School on Open Science Cloud Perugia, June 2017 https://baltig.infn.it/giaco/201706_perugia_cpp What is C++ C++ is a programming language that is:

More information

2

2 STL Design CSC 330 2 3 4 5 Discussions Associative Containers 6 7 Basic Associative Containers 8 set class 9 map class 10 Example multiset (1) 11 Example multiset (2) 12 Example multiset (3) 13 Example

More information

Concurrency and Parallelism with C++17 and C++20. Rainer Grimm Training, Coaching and, Technology Consulting

Concurrency and Parallelism with C++17 and C++20. Rainer Grimm Training, Coaching and, Technology Consulting Concurrency and Parallelism with C++17 and C++20 Rainer Grimm Training, Coaching and, Technology Consulting www.modernescpp.de Concurrency and Parallelism in C++ Concurrency and Parallelism in C++17 Parallel

More information

19.1 The Standard Template Library

19.1 The Standard Template Library Chapter 19: The Template Library From a review of Effective STL : 50 Specific Ways to Improve Your Use of the Standard Template Library by ScottMeyers: It s hard to overestimate the importance of the Standard

More information

Foundations of Programming, Volume I, Linear structures

Foundations of Programming, Volume I, Linear structures Plan 1. Machine model. Objects. Values. Assignment, swap, move. 2. Introductory algorithms: advance, distance, find, copy. Iterators: operations, properties, classification. Ranges and their validity.

More information

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

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming What the heck is STL???? Another hard to understand and lazy to implement stuff? Standard Template Library The standard template

More information

MODULE 37 --THE STL-- ALGORITHM PART V

MODULE 37 --THE STL-- ALGORITHM PART V MODULE 37 --THE STL-- ALGORITHM PART V My Training Period: hours Note: Compiled using Microsoft Visual C++.Net, win32 empty console mode application. g++ compilation example is given at the end of this

More information

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science Containers: Queue and List Jordi Cortadella and Jordi Petit Department of Computer Science Queue A container in which insertion is done at one end (the tail) and deletion is done at the other end (the

More information

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

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure

More information

Standard Template Library. Outline

Standard Template Library. Outline C++ Standard Template Library Spring 2015 Yanjun Li CS2200 1 Outline Standard Template Library Containers & Iterators STL vector STL list STL stack STL queue Fall 2008 Yanjun Li CS2200 2 Software Engineering

More information

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

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation Outline EDAF30 Programming in C++ 4. The standard library. Algorithms and containers. Sven Gestegård Robertz Computer Science, LTH 2018 1 Type inference 2 3 The standard library Algorithms Containers Sequences

More information

GridKa School 2013: Effective Analysis C++ Standard Template Library

GridKa School 2013: Effective Analysis C++ Standard Template Library GridKa School 2013: Effective Analysis C++ Standard Template Library Introduction Jörg Meyer, Steinbuch Centre for Computing, Scientific Data Management KIT University of the State of Baden-Wuerttemberg

More information

MODULE 35 --THE STL-- ALGORITHM PART III

MODULE 35 --THE STL-- ALGORITHM PART III MODULE 35 --THE STL-- ALGORITHM PART III My Training Period: hours Note: Compiled using Microsoft Visual C++.Net, win32 empty console mode application. g++ compilation example is given at the end of this

More information

GJL: The Generic Java Library

GJL: The Generic Java Library GJL: The Generic Java Library An STL for Java Winter 1999 Semester Project Laboratoire des Méthodes de Programmation Ecole Polytechnique Fédérale de Lausanne Corine Hari February 2000 Table of Contents

More information

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

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC CMSC 341 Lecture 6 STL, Stacks, & Queues Based on slides by Lupoli, Dixon & Gibson at UMBC Templates 2 Common Uses for Templates Some common algorithms that easily lend themselves to templates: Swap what

More information

Structuur van Computerprogramma s 2

Structuur van Computerprogramma s 2 Structuur van Computerprogramma s 2 dr. Dirk Deridder Dirk.Deridder@vub.ac.be http://soft.vub.ac.be/ Vrije Universiteit Brussel - Faculty of Science and Bio-Engineering Sciences - Computer Science Department

More information

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

G52CPP C++ Programming Lecture 18. Dr Jason Atkin G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1 Last lecture Operator Overloading Strings and streams 2 Operator overloading - what to know Know that you can change the meaning of operators Know that

More information

Exceptions, Templates, and the STL

Exceptions, Templates, and the STL Exceptions, Templates, and the STL CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 16 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/

More information

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS 1 CSE 100: C++ TEMPLATES AND ITERATORS 2 Announcements Look out for the extra weekend section More on git and C++ (iterators) Live demo by your friendly tutors Not mandatory but it will be fun Bring your

More information

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS CSE 100: C++ TEMPLATES AND ITERATORS Announcements Gradesource and clickers: We ll be making one more pass for unregistered clickers tonight, but after that you ll be on your own How is Assignment 1 going?

More information

Document Number: P0429R4 Date: Reply to: 0.1 Revisions... 1

Document Number: P0429R4 Date: Reply to: 0.1 Revisions... 1 Document Number: P0429R4 Date: 2018-05-05 Reply to: Zach Laine whatwasthataddress@gmail.com Audience: LWG A Standard flat_map Contents Contents i 0.1 Revisions.............................................

More information

List Iterator Implementation

List Iterator Implementation List Iterator Implementation Lecture 28 Section 14.6 Robb T. Koether Hampden-Sydney College Fri, Apr 10, 2015 Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 1 /

More information

List, Stack, and Queues

List, Stack, and Queues List, Stack, and Queues R. J. Renka Department of Computer Science & Engineering University of North Texas 02/24/2010 3.1 Abstract Data Type An Abstract Data Type (ADT) is a set of objects with a set of

More information

Computational Physics

Computational Physics Computational Physics numerical methods with C++ (and UNIX) 2018-19 Fernando Barao Instituto Superior Tecnico, Dep. Fisica email: fernando.barao@tecnico.ulisboa.pt Computational Physics 2018-19 (Phys Dep

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing STL and Iterators Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de Summer Semester

More information

Generic Programming with JGL 4

Generic Programming with JGL 4 Generic Programming with JGL 4 By John Lammers Recursion Software, Inc. July 2004 TABLE OF CONTENTS Abstract...1 Introduction to Generic Programming...1 How does Java support generic programming?...2 The

More information

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

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory C++ 프로그래밍실습 Visual Studio 2015 Contents STL Exercise Practice1 STL Practice 1-1 : Sets The insert method of set class ensures that the set does not contain duplicate keys Without specified position s.insert(123);

More information

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18 G52CPP C++ Programming Lecture 18 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Welcome Back 2 Last lecture Operator Overloading Strings and streams 3 Operator overloading - what to know

More information

Computer Application and Practice 2: Templates and the Standard Template Library

Computer Application and Practice 2: Templates and the Standard Template Library Computer Application and Practice 2: Templates and the Standard Template Library 2014 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Basics on templates Template classes Template

More information

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

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 4 (Based on Paul Kube course materials) Lecture 4 Binary search trees Toward a binary search tree implementation using C++ templates Reading: Weiss Ch 4, sections

More information

Class string and String Stream Processing Pearson Education, Inc. All rights reserved.

Class string and String Stream Processing Pearson Education, Inc. All rights reserved. 1 18 Class string and String Stream Processing 2 18.1 Introduction C++ class template basic_string Provides typical string-manipulation operations Defined in namespace std typedefs For char typedef basic_string

More information

primer 2005/1/19 18:36 page 843 #865

primer 2005/1/19 18:36 page 843 #865 primer 2005/1/19 18:36 page 843 #865 Index Bold face numbers refer to the page on which the term was first defined. Numbers in italic refer to the Defined Terms section in which the term is defined....

More information

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

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 the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue

More information

Standard Template Library

Standard Template Library Standard Template Library The standard template library (STL) contains CONTAINERS ALGORITHMS ITERATORS A container is a way that stored data is organized in memory, for example an array of elements. Algorithms

More information

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

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness. Iterators Overview We have introduced, used, built, and studied iterators in several contexts, including List, TDeque, and TVector. We have seen that ordinary pointers also can be thought of as iterators

More information

STL Standard Template Library

STL Standard Template Library 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

More information

1. The term STL stands for?

1. The term STL stands for? 1. The term STL stands for? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d 2. Which of the following statements regarding the

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

Abstract Data Types 1

Abstract Data Types 1 Abstract Data Types 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2 Abstract Data Types (ADTs) ADT is a set of objects together with a set of operations. Abstract in that implementation of operations

More information

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

C++ 11 and the Standard Library: Containers, Iterators, Algorithms and the Standard Library:,, Comp Sci 1575 Outline 1 2 3 4 Outline 1 2 3 4 #i n clude i n t main ( ) { i n t v a l u e 0 = 5 ; // C++ 98 i n t v a l u e 1 ( 5 ) ; // C++ 98 i n t v a

More information

7 TEMPLATES AND STL. 7.1 Function Templates

7 TEMPLATES AND STL. 7.1 Function Templates 7 templates and STL:: Function Templates 7 TEMPLATES AND STL 7.1 Function Templates Support generic programming functions have parameterized types (can have other parameters as well) functions are instantiated

More information

TEMPLATES AND ITERATORS

TEMPLATES AND ITERATORS TEMPLATES AND ITERATORS Problem Solving with Computers-I https://ucsb-cs24-sp17.github.io/ 2 Announcements Checkpoint deadline for pa04 (aka lab05) is due today at 11:59pm Be sure to push your code to

More information

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

TABLE OF CONTENTS...2 INTRODUCTION...3 C++ Advanced Features Trenton Computer Festival May 4 tth & 5 tt h, 2002 Michael P. Redlich Systems Analyst ExxonMobil Global Services Company michael.p.redlich@exxonmobil.com Table of Contents TABLE OF

More information

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! Winter 2013 Instructor: Hassan Khosravi Problems with Arrays and Vectors With arrays and vectors you are allocated a large space.

More information

Introducing C++ to Java Programmers

Introducing C++ to Java Programmers Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003 1 Philosophy of C++ Bjarne Stroustrup invented C++ in the early 1980's at Bell Laboratories First called "C with classes" Design Goals:

More information

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL) Chapter 16: Exceptions, Templates, and the Standard Template Library (STL) 6.1 Exceptions Exceptions Indicate that something unexpected has occurred or been detected Allow program to deal with the problem

More information

Module 9. Templates & STL

Module 9. Templates & STL Module 9 Templates & STL Objectives In this module Learn about templates Construct function templates and class templates STL 2 Introduction Templates: enable you to write generic code for related functions

More information

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

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University Abstract Data Types CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues

More information

Vectors. CIS 15 : Spring 2007

Vectors. CIS 15 : Spring 2007 Vectors CIS 15 : Spring 2007 Functionalia Midterm 2 : Ave. 58% (36.9 / 68) Med. 67% (45.5 / 68) Today: Standard Template Library Vector Template Standard Template Library C++ offers extended functionality

More information

Advanced C++ STL. Tony Wong

Advanced C++ STL. Tony Wong Tony Wong 2017-03-25 C++ Standard Template Library Algorithms Sorting Searching... Data structures Dynamically-sized array Queues... The implementation is abstracted away from the users The implementation

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information

Abstract Data Types 1

Abstract Data Types 1 Abstract Data Types 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2 Primitive vs. Abstract Data Types Primitive DT: programmer ADT: programmer Interface (API) data Implementation (methods) Data

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information