computer programmingcs101/2014.2/lecture... · •object-oriented programming with structures and...
TRANSCRIPT
![Page 1: Computer Programmingcs101/2014.2/lecture... · •Object-oriented programming with structures and classes •C++ Standard Library •The “string”class •The “vector” class](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022071212/60247e2334f809265d6cd683/html5/thumbnails/15.jpg)
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