The Standard Template Library Classes

Similar documents
List Iterator Implementation

Standard Library Reference

Programming with Haiku

Making std::vector constexpr

Minimal Spanning Trees

Stacks and their Applications

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Inheritance: The Fundamental Functions

CS197c: Programming in C++

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Scope and Parameter Passing

Inheritance: The Fundamental Functions

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

CSI33 Data Structures

Scope and Parameter Passing

Binary Tree Applications

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Critical-Path Algorithm

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Standard Template Library

Concepts for the C++0x Standard Library: Containers

The Decreasing-Time Algorithm

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

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

Implementing Linked Lists

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Containers in C++ and Java

Lectures 11,12. Online documentation & links

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

The Class Construct Part 2

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

Binary Tree Implementation

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Dynamic Allocation of Memory

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Function Templates. Consider the following function:

The Class Construct Part 1

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

MODULE 33 --THE STL-- ALGORITHM PART I

Binary Tree Implementation

CPSC 427a: Object-Oriented Programming

Dynamic Allocation of Memory

SSE2034: System Software Experiment 3

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

7 TEMPLATES AND STL. 7.1 Function Templates

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

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Function Definition Syntax Tree

Programming in C++ using STL. Rex Jaeschke

Unit 4 Basic Collections

Friends and Unary Operators

Solving Recursive Sequences by Iteration

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

The Graphics Pipeline

Recursive Descent Parsers

Standard Template Library. Outline

Module 9. Templates & STL

Recursive Linked Lists

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

Vectors. CIS 15 : Spring 2007

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

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Sampling Distribution Examples Sections 15.4, 15.5

Rotations and Translations

Scheduling and Digraphs

CS11 Advanced C++ Fall Lecture 1

The Pairwise-Comparison Method

Advanced Programming in C++ Container Design I

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

STL Containers, Part I

Density Curves Sections

COEN244: Class & function templates

XQuery FLOWR Expressions Lecture 35

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Coefficient of Determination

Building the Abstract Syntax Trees

The Projection Matrix

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

The Standard Template Library. An introduction

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

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

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Traveling Salesman Problem Nearest-Neighbor Algorithm

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

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

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

STL Exception Handling Contract

19.1 The Standard Template Library

The Graphics Pipeline

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

by Pearson Education, Inc. All Rights Reserved. 2

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

STL

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Dodatak D Standardna string klasa

Transcription:

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 Wed, Apr 23, 2014 1 / 33

1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 2 / 33

Outline 1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 3 / 33

The Standard Template Library The Standard Template Library (STL) was added to the C++ standard in 1994. It contains Container classes - Objects that hold collections of objects. Iterators - Objects that iterate through containers. Generic algorithms - Objects that perform standard procedures on objects. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 4 / 33

Outline 1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 5 / 33

The Container Classes The basic container classes are vector deque (double-ended queue) list The adaptor classes use container classes. The adaptor classes are stack queue priority_queue Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 6 / 33

The Container Classes Other container classes set multiset map multimap bitset Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 7 / 33

The Container Classes Visit the website http://www.cplusplus.com/reference/ for a full description of the STL. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 8 / 33

Outline 1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 9 / 33

The vector Container Class vector Constructors vector(); vector(vector& v); vector(size_type sz, T& value); vector(iterator first, iterator last); vector() Constructs the default vector. vector(vector) Constructs the copy vector. vector(sz, value) Constructs a vector of size sz, filled with value. vector(first, last) Constructs a vector with range of values given by the iterators first and last. We will use the vector class as an example. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 10 / 33

The vector Container Class The Assignment Operator vector& operator=(const vector& v); operator=() Assigns one vector to another. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 11 / 33

The vector Container Class The Capacity Functions size_type size() const; size_type max_size() const; void resize(size_type sz, T value); size_type capacity() const; bool empty() const; void reserve(size_type cap); size() Returns the number of element in the vector. max_size() Returns the maximum size possible. resize() Changes the size to sz, filling in with value. capacity() Returns current capacity. empty() Determines whether the vector is empty. reserve() Sets the capacity to cap. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 12 / 33

The vector Container Class Element Access Functions reference operator[](size_type i); reference at(size_type i); reference front(); reference back(); operator[]() Returns the element in position i. at() Same as operator[](i), but with range checking. front() Returns the element in the first position. back() Returns the element in the last position. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 13 / 33

The vector Container Class Mutator Functions void assign(size_type n, const T& value); void assign(iterator first, iterator last); void push_back(const T& value); void pop_back(); assign(n, value) Replaces the contents with n copies of value. assign(first, last) Replaces the contents with the range of values from first to last. push_back(value) Appends value to the end of the vector. pop_back() Removes the last element. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 14 / 33

The vector Container Class Mutator Functions iterator insert(iterator it, const T& value); void insert(iterator it, size_type n, const T& value); void insert(iterator it, iterator first, iterator last); insert(it, value) Inserts value in position given by the iterator it. insert(it, n, value) Inserts n copies of value starting in position given by the iterator it. insert(it, first, last) Starting in position given by iterator it, inserts values in range given by iterators first and last. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 15 / 33

The vector Container Class Mutator Functions iterator erase(iterator it); iterator erase(iterator first, iterator last); void swap(vector& v); void clear(); erase(it) Removes element in position given by the iterator it. erase(first, last) Removes range of elements given by the iterators first and last. swap() Swaps this vector with the given vector. clear() Removes all elements. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 16 / 33

The vector Container Class Iterator Functions iterator begin(); iterator end(); reverse_iterator rbegin(); reverse_iterator rend(); begin() Returns iterator set to beginning. end() Returns iterator set to end. rbegin() Returns reverse iterator set reverse beginning. rend() Returns reverse iterator set to reverse end. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 17 / 33

The vector Container Class Programming with vectors Write a program that creates a vector of ints, adds some ints to it, and and then prints the list. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 18 / 33

The vector Container Class Programming with vectors #include <vector> int main() { vector<int> vec; vec.push_back(10); vec.push_back(20); vec.push_back(30); vector<int>::iterator it; for (it = vec.begin(); it!= vec.end(); it++) cout << *it << endl; } Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 19 / 33

Example Test the vector Class The test program STLVectorTest.cpp. The executable STLVectorTest.exe. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 20 / 33

Outline 1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 21 / 33

The stack Adaptor Class An adaptor class uses a container class. We may construct a stack in any of the following ways. Ways to Construct a Stack #include <stack> int main() { stack<int> s1; stack<int, vector<int> > s2; stack<int, deque<int> > s3; stack<int, list<int> > s4; } Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 22 / 33

The stack Adaptor Class The stack class has the following member functions (besides the fundamental four). stack Member Functions bool empty() const; int size() const; T& top(); void push(const T& value); void pop(); Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 23 / 33

Outline 1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 24 / 33

The map Container Class A map is an associative list. Each member has A key. A value. The key must be unique for that member. The value is accessed through the key, by matching the key. This sounds like a hash table. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 25 / 33

The map Container Class Suppose we want to store a list of students and their declared majors. Name John Tim Betty Ann Major Mathematics Computer Science Chemistry Mathematics Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 26 / 33

The map Container Class If we intend to locate members by name, then The name is the key The major is the value. We construct the (empty) map: Construct a map #include <map> map<string, string> major; Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 27 / 33

The map Container Class To add the data, we may use the subscript operator: Initialize the map major["john"] = "Mathematics"; major["tim"] = "Computer Science"; major["betty"] = "Chemistry"; major["ann"] = "Mathematics"; Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 28 / 33

The map Container Class To find "John", we use the find() function. It returns an iterator to John s location in the map. Search the map map<string, string>::iterator it; it = major.find("john"); Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 29 / 33

The map Container Class The data members first and second store the key and the value. Print the map map<string, string>::iterator it; for (it = major.begin(); it!= major.end(); it++) cout << it.first << " is majoring in " << it.second << endl; Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 30 / 33

The map Container Class Test the map Class The test program STLMapTest.cpp. The executable STLMapTest.exe. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 31 / 33

Outline 1 The Standard Template Library 2 The Container Classes 3 The vector Class 4 The stack Class 5 The map Class 6 Assignment Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 32 / 33

Assignment Homework Read Section 9.7, pages 503-510. Read Section 9.8, pages 511-515. Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes Wed, Apr 23, 2014 33 / 33