Exercise 6.2 A generic container class

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

COEN244: Class & function templates

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

by Pearson Education, Inc. All Rights Reserved. 2

Copy Constructors & Destructors

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Chapter 17 vector and Free Store. Bjarne Stroustrup

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

Absolute C++ Walter Savitch

CS 211 Programming Practicum Fall 2018

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

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Ch. 11: References & the Copy-Constructor. - continued -

Dynamic Data Structures

CSI33 Data Structures

STL Quick Reference for CS 241

Vector and Free Store (Vectors and Arrays)

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CS201 Some Important Definitions

CPSC 427: Object-Oriented Programming

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Chapter 17 vector and Free Store

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

Ch. 12: Operator Overloading

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

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

Chapter 17 vector and Free Store

1. The term STL stands for?

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

pointers & references

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

C++ Templates. David Camp

Financial computing with C++

CSC 210, Exam Two Section February 1999

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Instantiation of Template class

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

Chapter 19 Vector, templates, and exceptions

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup

Short Notes of CS201

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

Vector and Free Store (Pointers and Memory Allocation)

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS201 - Introduction to Programming Glossary By

Cpt S 122 Data Structures. Templates

Advanced Systems Programming

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Introduction to C++ with content from

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

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Abstract. Vector. Overview. Building from the ground up. Building from the ground up 1/8/10. Chapter 17 vector and Free Store

The vector Class and Memory Management Chapter 17. Bjarne Stroustrup Lawrence "Pete" Petersen Walter Daugherity Fall 2007

C and C++ 7. Exceptions Templates. Alan Mycroft

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

SFU CMPT Topic: Class Templates


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

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

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

C++ Programming Lecture 7 Software Engineering Group

Pointers. Developed By Ms. K.M.Sanghavi

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 5. The Standard Template Library.

CS201 Latest Solved MCQs

CS11 Advanced C++ Fall Lecture 7

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

STL: C++ Standard Library

CS 103 Unit 12 Slides

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

C++ (Non for C Programmer) (BT307) 40 Hours

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CSE 333 Midterm Exam 5/10/13

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

Dynamic Storage Exercise

CS 211. Project 5 Infix Expression Evaluation

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Problem Solving with C++

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Object-Oriented Principles and Practice / C++

Common Misunderstandings from Exam 1 Material

use static size for this buffer

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Programming in C and C++

Exam 3 Chapters 7 & 9

System Security Class Notes 09/23/2013

Object-Oriented Programming, Iouliia Skliarova

Lecture on pointers, references, and arrays and vectors

+ Abstract Data Types

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Transcription:

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 class ex6.2/array.hh that implements a simple implementation of an array of double values. Convince yourself that the class correctly implements the constructor, destructor, copy constructor and assignment operator. b) Change the class Array into a template class Array. c) Use class Array in a small test program to store an array of integers and to store an array of const char* strings. d) Does the code in operator[] look safe to you? What happens if you try to access element 1000 of an array of length 10? e) Change the operator[] such that when an element beyond the range of array is accessed, the array is automatically extended to include that element using the resize() function. f) A drawback of the resize() operation is that the newly created elements do not have a defined value. Change the constructor of class Array such that it has takes two arguments: the initial capacity and the default value that is assigned to blank elements. Change the resize() function such that they use the specified default value to initialize all newly allocated elements.

Exercise 6.3 Revisiting the class Stack The goal of this exercise is to revisit the Stack class from exercise 3.3 and revisit its storage strategy from a raw C++ array into the use of the smart class Array. a) Copy the provided solution for the Stack class from exercise 3.3, its main program as well as the Array class from exercise 6.2. Compile the code and verify that works OK. b) We will now rewrite class Stack to use class Array for internal storage. We start with the data members of class Stack: In the solution of Ex. 3.3, the data is stored using an array of doubles (double *s) and an associated length (int len). Replace these with a Array<double> s. Don t forget to include the Array.hh header file in Stack.hh. c) There are several places in the code of Stack that use the length of the internal memory buffer that used to be stored in len. This information is now available from Array<double> s, so replace each occurrence of len in the code with s.size(), which reports the size of the buffer in s. d) Do you still need an explicit copy constructor and destructor for class Stack? Hint: do you still have pointers to owned memory as data members?. If not, remove them. e) Adapt the constructor to initialize all data members: s with given size and counter with value zero. Eliminate function init() since it is now superfluous.

Exercise 6.3 Revisiting the class Stack f) Remove grow() entirely since its functionality is now mostly absorbed in Array. In push() replace grow() with s.resize(s.size()+10). g) Test the modified Stack class with the main program to verify that it still works. h) As a final step, turn class Stack into a template class Stack. To do so you need to change any reference to type double to a generic type T in the code and add a template<class T> line to the class declaration.

Exercise 7.1 Make a catalogue of English words Online STL reference: http://www.sgi.com/tech/stl/ The goal of this exercise is to use C++ STL classes to make a catalogue of all the English words that occur in a given text file, including the number of times that each word occurs. a) Write a small main() program that reads in file ex5.3/example.txt word by word. Open the file using an ifstream class and read each word into a STL class string using operator>>. As a first step in the development of your program, print each word as your read it. b) The next step is to use an STL map to keep track of all the words. Create an object name mymap of the type map<string,int> in the beginning of your program that will hold the catalogue of words. Then replace the line of code in your reading loop that prints out the word with a line of code that stores it: mymap[word] += 1. Explain what the preceding line of code does. c) Finally, at the end of your program once you have processed all words in the file print out the contents of mymap using the iterator mechanism: First create an iterator that points to the first element of mymap using the begin() method. Then create a while() loop that compares the iterator to the value of mymap.end(), which returns an iterator pointing to the last element in the map(). Inside the loop, print out each map element. Remember that a map stores a pair of values and the iterator points to a pair object, which has data member first and second that hold the key and value of each map entry. Finally, use the ++ operator to move the iterator to the next element inside the while loop.

Exercise 7.2 A palindrome tester Online STL reference: http://www.sgi.com/tech/stl/ The goal of this exercise is to write a program that checks if a vector is a palindrome A palindrome is a sequence of values that reads the same forward as backward (e.g. 1,2,3,2,1) Approach vector<int> palindromes a) Create a small main() program that allocates a vector<int> b) Now write a piece of code that checks if that vector is a palindrome. To do so use two iterators: one that starts at the front and is incremented and one that starts at the end and is decremented. Compare elements pointed to by both iterators at each step, if there is a mismatch the vector is not a palindrome Function begin() and end() return iterators starting at the begin and end respectively. Note that the iterator return by end() is positioned beyond the end of the collection you need to decrement it once to put it on the last element. Approach vector<t> palindromes a) Write a template function bool ispalindrome(vector<t> v) that can perform the check for a vector of any type You can ignore any g++ compiler warnings about implicit type that you may get WE NEED TO FIX THIS: THIS IS NOW A COMPILER ERROR!!!