computer programmingcs101/2014.2/lecture... · •object-oriented programming with structures and...

15
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session : Template Class “vector” – Part 2 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Upload: others

Post on 02-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Computer ProgrammingDr. Deepak B Phatak

Dr. Supratik ChakrabortyDepartment of Computer Science and Engineering

IIT Bombay

Session : Template Class “vector” – Part 2

1Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Page 2: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

• Object-oriented programming with structures and classes

• C++ Standard Library• The “string” class

• The “vector” class – some features of it

2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics

Page 3: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

• More features of the template class “vector”

3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture

Page 4: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Acknowledgment

• Much of this lecture is motivated by the treatment in

An Introduction to Programming Through C++

by Abhiram G. Ranade

McGraw Hill Education 2014

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4

Page 5: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

The “vector” class

• For representing and manipulating one dimensional arrays of objects

• Uses dynamically allocated array to store elements

Array can grow or shrink in size

• Dynamic memory management built in

• “vector” objects are container objects

• Must use #include <vector> at start of program

• Large collection of member functions• We’ll see only a small subset

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5

Page 6: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Vectors of Complex Data Types

• “vector” is a template class• So why restrict to vector<int>, vector<float>, vector<char> … ?

• Recall

• We can have vector<V3> v3Vector(10);

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6

class V3 {private: double x, y, z;

double length() const { … }public: operator+(V3 const &b) const {…}

… Other member functions …};

Page 7: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Vectors of Complex Data Types

• Vectors of pointers

vector<int *> intPtrVec;

vector<V3 **> v3PtrPtrVec;

• Vectors of vectors !

vector <vector<int> > x;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7

Each element of intPtrVec is an object

of type int *

Each element of v3PtrPtrVec is an

object of type V3**

Each element of x is a vector of integers

Page 8: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Multi-dimensional Vectors

• Vectors of pointers

vector<int *> intPtrVec;

vector<V3 **> v3PtrPtrVec;

• Vectors of vectors !

vector <vector<int> > x;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8

Note the space

Page 9: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Multi-dimensional Vectors

vector<vector<int> > x(4, vector<int>(9));

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9

Vector of (vector of integers)

x[0]

x[1]

x[2]

x[3]

Page 10: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Multi-dimensional Vectors

vector<vector<int> > x(4, vector<int>(9));

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10

Vector of (vector of integers)

x[0]

x[1]

x[2]

x[3]

Access: (x[2])[3] or simply x[2][3]

Page 11: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Multi-dimensional Vectors

vector<vector<int> > x(4);

x[0] = vector<int>(1);

x[1] = vector<int>(2);

x[3] = vector<int>(3);

x[4] = vector<int>(4);

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11

Lower triangular matrix

x[0]

x[1]

x[2]

x[3]

Page 12: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

A User-defined Matrix Class using Vectors

class matrix2D {

private: vector<vector<int> > elements;

public:

matrix2D(size_t m, size_t n) : elements(m, vector<int>(n)) { }

int &operator() (size_t i, size_t j) { return elements[i][j]; }

size_t nrows() { return elements.size(); }

size_t ncols() {return elements[0].size(); }

};

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12

alias for unsigned int

Preferred type for sizes of objects in

memory

Also used for indices in string, vector, …

Page 13: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

A User-defined Matrix Class using Vectors

class matrix2D {

private: vector<vector<int> > elements;

public:

matrix2D(size_t m, size_t n) : elements(m, vector<int>(n)) { }

int &operator() (size_t i, size_t j) { return elements[i][j]; }

size_t nrows() { return elements.size(); }

size_t ncols() {return elements[0].size(); }

};

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13

Overloaded () operator

Page 14: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

A User-defined Matrix Class using Vectors

int main() {

matrix2D M(5, 5);

for (size_t i = 0; i < M.nrows(); i++) {

for (size_t j = 0; j < M.ncols(); j++) {

M(i, j) = i*i + j*j;

}

}

… Some other code …

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14

Page 15: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class

IIT Bombay

Summary

• Additional features of the “vector” class

• Multi-dimensional vectors

• Many more member functions of the “vector” class exist

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15