Chapter 5. The Standard Template Library.

Size: px
Start display at page:

Download "Chapter 5. The Standard Template Library."

Transcription

1 Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov, Meng Lee, and David Musser at Hewlett-Packard Research Labs in the early nineties. Today STL becomes a part of the Standard C++ Library together with I/O streams, the C standard library, etc. STL is a general purpose library of generic algorithms and generic data structures - containers that can be plugged together and used in an application. Special STL components, called iterators and being generalized C/C++ pointers, connect containers and algorithms and allow them working together effectively. The STL containers include the most commonly used and most valuable data structures, such as vectors, deques, lists, sets, and maps. The template algorithm components include a broad range of fundamental algorithms for the most common kinds of data manipulations, such as searching, sorting, and merging. The STL generally avoids inheritance and virtual functions in favour of using templates. STL contains six major kinds of components: first of all, three key STL components - containers, generic algorithms, and iterators, as well as three auxiliary ones - function objects, adapters, and allocators ( Fig. 5.1). Adaptors Containers Function objects Iterators Allocators Algorithms Fig The STL components and their main relationships. All STL components are defined in the namespace std. Containers are used to manage collections of objects of certain type. Different containers have different abilities, such as internal data structure (arrays, linked lists, binary trees), category of iterators provided, effectiveness of inserting/removing elements, and so on. Containers have own member functions to provide some restricted processing own elements (inserting/removing elements, etc.). Iterators are used to step through the elements of collections of objects. These collections may be containers or subset of containers. Iterators offer common interface for any arbitrary container type. For example, the operator++ lets the iterator step to the next element in the collection. This is done independently of the internal structure of the collection (array, tree, etc). This is because every container class provides its own iterator type being actually a kind of smart pointer that translates the call "go to the next element" into whatever is appropriate. Algorithms are used to provide broad scope of processing the elements of collections. For example, they can search, sort, or simply use the elements for different purposes.

2 Object-oriented programming B, Lecture 11e. 2 Algorithms use iterators, therefore an algorithm has to be written only once to work with arbitrary containers because the iterator interface is common for all container types. Most of the STL algorithms are provided in the standard header <algorithm>, and some in the header <numeric>. Function objects are used in algorithms as functional arguments, and in sorted associative containers as template parameters. The STL function objects covers fundamental operations: arithmetic, relational, and logical. They are provided in the standard header <functional>. Adaptors are used for modifying the interface of another components - containers, iterators, and function objects. For example, the container adaptors using the fundamental container classes are actually new containers with specific abilities: stack, queue, and priority queue. Another example is so-called binders - a category of function adaptors that help us construct a wider variety of function objects. Allocators are used in STL instead of new and delete for the allocation and deallocation of memory and represent a special memory model. Every STL container class uses a class Allocator to encapsulate information about the memory model used: pointers/constant pointers, references/constant references, sizes of objects, allocation and deallocation functions, etc. The STL defines a default allocator being sufficient in most programs Introduction to the STL containers. In STL, containers are objects that store collections of other objects, all of the same type T. There are two categories of fundamental (so-called first-class) STL container types: - sequence containers; - sorted associative containers. The STL provides also three above-mentioned container adaptor classes - stack, queue, and priority_queue that modify (adapt) the interface of an underlying first-class container to behave in a particular way (e.g., stack adaptor creates a LIFO list) Sequence containers. Computational complexity. Sequence containers are ordered collections in which every element has a certain position. This position depends on the time and place of the insertion, but is independent of the value of the element. That is, sequence containers provide a strictly linear arrangement that preserves the relative positions into which the elements are inserted. A few words about terminology. A sequence (often called a range) is fixed by the pair of iterators: first iterator refers to the first element of the sequence, and the second refers to a one-beyond-the-last hypothetical element (Fig. 5.2). Such a sequence is called half open, and traditional mathematical notation for a half-open range is [first, last). next position after the end of the container first last Fig A sequence (range) delimited by the pair of iterators. For algorithms that work on containers of size n, we can consider the maximum time (or the worst-case time) T(n) that an algorithm could take. Instead of trying to develop a precise formula for T(n), we can find a simple asymptotic function that provides an upper bound on the function T(n). For example, we might have the following inequality: T(n) cn,

3 Object-oriented programming B, Lecture 11e. 3 for some constant c and all sufficiently large n (say, for n N ). This relation can be written using so-called big Oh notation: T(n) = O(n). In this case algorithm is said to have a linear time bound or, more simply, to be linear time. If T(n) c (or T(n) = O(1)), the algorithm is said to have a constant time bound or to be constant time. In this case we can talk about so-called random access. It means that the time to reach the nth element of the sequence is constant, i.e., time does not depend on n. The following graphs illustrate these cases: T c*n T - running time T(n) c c T(n) N Fig Asymptotic estimations of the computational complexity n a) constant time bound b) linear time bound N n The STL first-class sequence container types are as follows: - vector<t>, providing random access to any element of a sequence of varying length, with rapid (constant time) insertions and deletions at the end; - deque<t>, (it's pronounced deck) also providing random access to any element of a sequence of varying length, with rapid insertions and deletions at front or back; - list<t>, doubly-linked list providing only linear-time (O(n))access to a sequence of varying length, but with rapid insertions and deletions at any position in the sequence. The header files for these containers are as follows: <vector>, <deque>, <list>. Actually, the STL involves two other sequence containers: - T a[n], that is, ordinary C++ array types, which provide random access to a sequence of fixed length n; - the class basic_string (or simply string) placed in the header <string>. Unlike ordinary arrays, class string provides the STL container interface. The class string objects can be considered as containers of characters Sorted associative containers. Sorted associative containers provide ability for fast retrieval of objects from the collection based on keys that stored in the item (or in some cases are the items themselves). The sorted associative containers are based on one general approach to associative retrieval, which is to keep keys sorted according to some total order, such as numeric order if the items are numbers or lexicographical order if the keys are strings, and use a binary search algorithm. These containers can be implemented with balanced binary search trees. (another approach is hashing.) STL has four sorted associative container types: - set<key>, which supports unique keys (contains at most one of each key value) and provides for fast retrieval of the keys themselves; therefore, each element serves as both a sort key and a value;

4 Object-oriented programming B, Lecture 11e. 4 - multiset<key>, which supports duplicate keys (possibly contains multiple copies of the same key value) and provides for fast retrieval of the keys themselves; - map<key,t>, which supports unique keys (of type Key) and provides for fast retrieval of another type T (mapped value) based on the keys (rapid key-based lookup); a map is also called an associative array. An associative array can be thought as an array for which the index need not be an integer. - multimap<key,t>, which supports duplicate keys (of type Key) and provides for fast retrieval of another type T based on the keys; multimap allows duplicate elements for a given key and is also called a dictionary. A good example of multimap could be a phone book, since one person can have several phone numbers. So, set and multiset can be seen as degenerate associative arrays/dictionary in which no value is associated with a key. Both map and multimap are contained in the header file <map>, as well as both set and multiset are contained in the header file <set> Common container operations. Each STL container has associated member functions, like push_back in the vector container. They ensure minimal functionality. Some of them are common for all containers. They include, in particular: default constructor to provide a default initialization of the container; copy constructor that initialize the container to be a copy of an existing container of the same type; operator = that assigns one container to another; empty that returns true if there are no elements in the container, false otherwise; size that returns the number of elements currently in the container, and so on. There are also member functions that are only found in first-class containers, such as begin - returns an iterator (or constant iterator) referring to the first element of the container; end - returns an iterator (or constant iterator) referring to the next position after the end of the container; erase - removes one or more elements from the container; clear that removes all elements from the container (makes the container empty). relational and equality operators. It is important to take into account, when an element is inserted into a container, a copy of that element is made. Therefore, if default member-wise copy does not perform a proper copy operation for the element type, the element type must provide its own copy constructor and assignment operator The STL class template pair. The definition of the class template pair is included in the STL header file <utility> containing several templates of general utility. Pair's most important members are shown here: template<class T1, class T2> struct std::pair { //type definitions: typedef T1 first_type; typedef T2 second_type; //data members: T1 first; T2 second;

5 Object-oriented programming B, Lecture 11e. 5 //default and parameterized constructors: pair(): first(t1()), second(t2()) {} pair(const T1& x, const T2& y): first(x), second(y) {} //the template version of a copy constructor with implicit //conversions opportunity: template<class U, class V> pair(const pair<u,v>& p) :first(p.first), second(p.second){} }; //convenience function to create a pair: template<class T1, T2> pair<t1,t2> make_pair(const T1&, const T2); A pair is by default initialized to the default values of its element types. For example, strings are initialized to the empty strings. A pair is used in a map/multimap containers, where the key is the first element of the pair and the mapped value is the second Vectors. Vectors are sequence containers that provide fast random access to any element of a sequence of varying length via the subscript operator [] exactly like a C/C++ "raw" array; fast insertions and deletions at the end are provided as well. The sequence is stored as an array of type T.. A vector models a dynamic array: Fig.5.4. Structure of the STL vector. The class declaration contains type definitions and member functions (incl. constructors) declarations in its public section: template<class T, class A = allocator<t> > class vector { public: //types definitions: //constructors: //access member functions and others: protected: A allocator; }; Vectors constructors. Vectors have actually 5 kinds of constructors: - the default constructor, i.e. a constructor function that can be called without supplying an argument, produces a sequence that is initially empty (contains no elements); it is used in expressions in the form: vector<t>(), e.g. vector<int>* pv = new vector<int>(); or in declarations in the form: vector<t> object_name; e.g. vector<person> myvector;

6 Object-oriented programming B, Lecture 11e. 6 - the parameterized constructors of three kinds: - the first one being used in expressions in the form vector<t>(n) or in a declaration in the form vector<t> object_name(n); actually, this kind of a parameterized constructor produces a sequence initialized with n copies of the result of calling the default constructor of type T, that is T(). - the second kind is expressed by vector<t>(n, value) or vector<t> object_name(n, value); it produces a sequence initialized with n copies of value, which must be of type T. - the third kind is declared as vector(const_iterator first, const_iterator last, const A& a1 = A()); that constructs a vector of size last - first and initializes it with copies of elements in the range [first, last) of another vector or array. - the copy constructor being declared as usual: vector(const vector& x); and used in statements like vector<double> v1(v0); // where v0 is an object of type // vector<double> or vector<student>* pvs = new vector<student>(v2); //where v2 is an object of type vector<student> The code below shows the usage of the constructors mentioned: Example : #pragma warning(disable:4786) // Disable warning message : #include<iostream> 03: #include<vector> 04: #include<string> 05: #include<cassert> 06: using namespace std; 07: 08: int main() 09: { 10: //using default constructor: 11: vector<char> v0; 12: assert(v0.size() == 0 && v0.empty() == true); 13: vector<int>* pv0 = new vector<int>(); 14: assert( pv0->empty() == true ); 15: 16: //using parameterized constructor - 1st kind: 17: vector<int> v1(3); 18: assert(v1.size() == 3 && v1[0] == int()); 19: 20: //using parameterized constructor - 2nd kind: 21: vector<string> v2(2, "data"); 22: cout << v2[0] << " " << v2[1] << endl; 23: assert(v2[0] == "data"); 24: 25: //using parameterized constructor - 3rd kind: 26: const int SIZE = 3; 27: int a[size] = {3, 5, 7}; 28: vector<int> v3(a, a + SIZE); 29: for(int i = 0; i < SIZE; i++) 30: cout << v3[i] << " "; // 3 5 7

7 Object-oriented programming B, Lecture 11e. 7 31: cout << endl; 32: 33: for(int* j = v3.begin(); j < v3.end(); j++) 34: cout << *j << " "; 35: cout << endl; // : 37: //using copy constructor: 38: vector<string> v4(v2); 39: vector<string>* pv4 = new vector<string>(v2); 40: assert(v4[0] == (*pv4)[0]); 41: cout << (*pv4)[1] << endl; // data 42: 43: //copy constructor and pair objects: 44: vector< pair<int, string> > v41; copy constructors are called 45: v41.push_back(pair<int, string>(1,"one")); 46: vector< pair<int, string> >* 47: pv41c = new vector< pair<int, string> >(v41); 48: cout << (*pv41c)[0].first << " " << (*pv41c)[0].second 49: << endl; //1 one 50: return 0; 51: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(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

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

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

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

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

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1 Lecture 12 Log into Linux. Copy files on csserver in /home/hwang/cs215/lecture12/*.* Reminder: Practical Exam 1 is Wednesday 3pm-5pm in KC-267. Questions about Project 2 or Homework 6? Submission system

More information

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

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11 Di erent Kinds of Containers Container Notes A container is an object that stores other objects and has methods for accessing the elements. There are two fundamentally di erent kinds of containers: Sequences

More information

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

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides. 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

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

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

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

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

STL Quick Reference for CS 241

STL Quick Reference for CS 241 STL Quick Reference for CS 241 Spring 2018 The purpose of this document is to provide basic information on those elements of the C++14 standard library we think are most likely to be needed in CS 241.

More information

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

Data Structures in C++ Using the Standard Template Library

Data Structures in C++ Using the Standard Template Library Data Structures in C++ Using the Standard Template Library Timothy Budd Oregon State University ^ ADDISON-WESLEY An imprint of Addison Wesley Longman, Inc. Reading, Massachusetts Harlow, England Menlo

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

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

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

SSE2034: System Software Experiment 3

SSE2034: System Software Experiment 3 SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu STL Collection Types Template based set of collection

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

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS CSE 100: C++ TEMPLATES AND ITERATORS Announcements iclickers: Please register at ted.ucsd.edu. Start ASAP!! For PA1 (Due next week). 10/6 grading and 10/8 regrading How is Assignment 1 going? A. I haven

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

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

Template based set of collection classes STL collection types (container types) STL Collection Types Template based set of collection classes STL collection types (container types) Sequences vector - collection of elements of type T list - doubly linked list, only sequential access

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

C and C++ 8. Standard Template Library (STL) Stephen Clark

C and C++ 8. Standard Template Library (STL) Stephen Clark C and C++ 8. Standard Template Library (STL) Stephen Clark University of Cambridge (heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford and Bjarne Stroustrup) Michaelmas

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

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...

More information

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators Review from Lecture 7 Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

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

Exercise 6.2 A generic container class

Exercise 6.2 A generic container class Exercise 6.2 A generic container class The goal of this exercise is to write a class Array that mimics the behavior of a C++ array, but provides more intelligent memory management a) Start with the input

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

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

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Standard Template Library 1 Standard Template Library Need to understand basic data types, so we build them ourselves Once understood, we

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

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Review from Lecture 7 CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8

More information

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

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators,

More information

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

When we program, we have to deal with errors. Our most basic aim is correctness, but we must Chapter 5 Errors When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with incomplete problem specifications, incomplete programs, and our own errors. When

More information

The Ada Standard Generic Library (SGL)

The Ada Standard Generic Library (SGL) The Ada Standard Generic Library (SGL) Alexander V. Konstantinou Computer Science Graduate Seminar 4/24/96 1 Presentation Overview Introduction (S{T G}L) The C++ Standard Template Library (STL) Ada 95

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

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

Review Questions for Final Exam

Review Questions for Final Exam CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.

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

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

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

Object-Oriented Programming

Object-Oriented Programming s iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 21 Overview s 1 s 2 2 / 21 s s Generic programming - algorithms are written with generic types, that are going to be specified later. Generic programming

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

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

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47 Lecture 8 Xiaoguang Wang STAT 598W February 13th, 2014 (STAT 598W) Lecture 8 1 / 47 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 2 / 47 Outline 1 Introduction: C++ 2 Containers

More information

The C++ Standard Template Library

The C++ Standard Template Library The C++ Standard Template Library CS 342: Object-Oriented Software Development Lab The C++ Standard Template Library David L. Levine Christopher D. Gill Department of Computer Science Washington University,

More information

Sequential Containers. Ali Malik

Sequential Containers. Ali Malik Sequential Containers Ali Malik malikali@stanford.edu Game Plan Recap Overview of STL Sequence Containers std::vector std::deque Container Adapters Announcements Recap getline vs >> Bug favnum = fullname

More information

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2 CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2 Review of Lecture 16 Maps are associations between keys and values. Maps have fast insert, access and remove operations:

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

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

C++ Programming Lecture 2 Software Engineering Group

C++ Programming Lecture 2 Software Engineering Group C++ Programming Lecture 2 Software Engineering Group Philipp D. Schubert Contents 1. Compiler errors 2. Functions 3. std::string 4. std::vector 5. Containers 6. Pointer and reference types Compiler errors

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

Copy Constructors & Destructors

Copy Constructors & Destructors Copy Constructors & Destructors 2-07-2013 Implementing a vector class local functions implicit constructors Destructors Copy Constructors Project #1: MiniMusicPlayer Quiz today 2/7/13 vectors and reading

More information

Sequential Containers. Ali Malik

Sequential Containers. Ali Malik Sequential Containers Ali Malik malikali@stanford.edu Game Plan Recap Stream wrapup Overview of STL Sequence Containers std::vector std::deque Container Adapters Announcements Recap stringstream Sometimes

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

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

More on Templates. Shahram Rahatlou. Corso di Programmazione++ More on Templates Standard Template Library Shahram Rahatlou http://www.roma1.infn.it/people/rahatlou/programmazione++/ it/ / h tl / i / Corso di Programmazione++ Roma, 19 May 2008 More on Template Inheritance

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

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

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers 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

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

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

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types Arrays - Vectors Data Types Data Type: I. set of values II. set of operations over those values Example: Integer I. whole numbers, -32768 to 32767 II. +, -, *, /, %, ==,!=, , =,... Which operation

More information

Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation

Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation Modular program development Step-wise refinement Interface, specification, and implementation Language support for modularity Procedural abstraction Abstract data types Packages and modules Generic abstractions

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured Introduction p. xxix The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured Language p. 6 C Is a Programmer's Language

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

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 Data that is formatted and written to a sequential file as shown in Section 17.4 cannot be modified without the risk of destroying other data in the file. For example, if the name White needs to be changed

More information

CS 32. Lecture 5: Templates

CS 32. Lecture 5: Templates CS 32 Lecture 5: Templates Vectors Sort of like what you remember from Physics but sort of not Can have many components Usually called entries Like Python lists Array vs. Vector What s the difference?

More information

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I Review from Lecture 15 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.

More information

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

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

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

The time and space are the two measure for efficiency of an algorithm.

The time and space are the two measure for efficiency of an algorithm. There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space

More information

Function Templates. Consider the following function:

Function Templates. Consider the following function: Function Templates Consider the following function: void swap (int& a, int& b) { int tmp = a; a = b; b = tmp; Swapping integers. This function let's you swap the contents of two integer variables. But

More information

Containers in C++ and Java

Containers in C++ and Java Containers in C++ and Java 1 1. Which of the following statements is true? a) Equality on the basis of reference implies equality on the basis of content. b) Equality on the basis of content implies equality

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

C++: Overview and Features

C++: Overview and Features C++: Overview and Features Richard Newman r.newman@rdg.ac.uk Room CS127 2003-12-11 Programming & Design, 2003 1 Introduction You have: used streams seen how classes are used seen some C++ code Today: good

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

PIC 10A. Lecture 23: Intro to STL containers

PIC 10A. Lecture 23: Intro to STL containers PIC 10A Lecture 23: Intro to STL containers STL STL stands for Standard Template Library There are many data structures that share similar features These data structures can be manipulated by a common

More information

Exam 3 Chapters 7 & 9

Exam 3 Chapters 7 & 9 Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False

More information

6.096 Introduction to C++

6.096 Introduction to C++ MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Massachusetts Institute

More information

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I Review from Lectures 17 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.

More information

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2 CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2 Review of Lecture 14 Maps are associations between keys and values. Maps have fast insert, access and remove operations:

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information