[go: up one dir, main page]

0% found this document useful (0 votes)
1 views20 pages

CS101x S450A Template Class Vector Part 1 IIT Bombay 1

The document provides an overview of the C++ template class 'vector', which is used for representing and manipulating one-dimensional arrays of objects. It covers various functionalities such as accessing elements, appending, deleting, and resizing vectors, along with examples of code snippets. The lecture is part of a course on computer programming at IIT Bombay, led by Dr. Deepak B. Phatak and Dr. Supratik Chakraborty.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views20 pages

CS101x S450A Template Class Vector Part 1 IIT Bombay 1

The document provides an overview of the C++ template class 'vector', which is used for representing and manipulating one-dimensional arrays of objects. It covers various functionalities such as accessing elements, appending, deleting, and resizing vectors, along with examples of code snippets. The lecture is part of a course on computer programming at IIT Bombay, led by Dr. Deepak B. Phatak and Dr. Supratik Chakraborty.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

IIT Bombay

Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session : Template Class “vector”

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


Quick Recap of Relevant Topics
IIT Bombay

• Object-oriented programming with structures and classes


• Template classes and functions
• C++ Standard Library
• The “string” class

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


Overview of This Lecture
IIT Bombay

• The template class “vector”

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


Acknowledgment
IIT Bombay

• 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


The “vector” class
IIT Bombay

• For representing and manipulating one dimensional arrays


of objects
• Template class: can be instantiated with specific type
• 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
Simple Programming using “vector”
IIT Bombay

#include <iostream>
#include <vector>
using namespace std;
int main() { 0 20
vector<int> intVec;
vector<float> floatVec(20);
vector<char> charVec(5, ‘a’);
cout << intVec.size() << “ “ << floatVec.size() << endl;
for (int i = 0; i < 5; i++) {cout << charVec[i];}
cout << endl; return 0;
} aaaaa
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6
Accessing Elements using [] and at
IIT Bombay

#include <iostream>
intVec[100] vs intVec.at(100):
#include <vector>
using namespace std;
Illegal memory/garbage access
int main() { vs
vector<int> intVec(5); out_of_range exception
int index;
for (int i = 0; i < 5; i++) { intVec[i] = i; } index: 3
cout << “Give an index: “; Value at index 3 is 3
cin >> index;
cout << “Value at index “ << index << “ is “ << intVec.at(index) << endl;
return 0;
}

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


Accessing Special Elements using front and back
IIT Bombay

#include <iostream>
#include <vector>
using namespace std; Front element is: 0
int main() { Back element is: 4
vector<int> intVec(5);
for (int i = 0; i < 5; i++) { intVec.at(i) = i;}
cout << “Front element is: “ << intVec.front() << endl;
cout << “Back element is: “ << intVec.back() << endl;
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8
Appending Element to a Vector
IIT Bombay

#include <iostream>
#include <vector> Initial size: 0
using namespace std;
int main() {
Final size: 5
vector<int> intVec;
cout << “Initial size: “ << intVec.size() << endl;
for (int i = 0; i < 5; i++) { intVec.push_back(i); }
cout << “Final size: “ << intVec.size() << endl;
for (int i = 0; i < 5; i++) { cout << intVec.at(i) << “ “; }
return 0;
} 01234
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9
Deleting Element From End of a Vector
IIT Bombay

#include <iostream>
#include <vector>
using namespace std;
int main() {
Size after
vector<int> intVec; pop back: 4
cout << “Initial size: “ << intVec.size() << endl;
for (int i = 0; i < 5; i++) { intVec.push_back(i); }
cout << “Final size: “ << intVec.size() << endl; 01 23
intVec.pop_back();
cout << “Size after pop back: “ << intVec.size() << endl;
for (int i = 0; i < 4; i++) { cout << intVec.at(i) << “ “;}
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10
Recall: C++ Iterator
IIT Bombay

• An object that points to an element in a collection of


elements, and can be used to iterate through the elements
in the collection
• Like a pointer, but not exactly the same
• Must support ++ (increment) and * (dereference) operations

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


Iterator Related Functions in “vector” Class
IIT Bombay

#include <iostream>
begin(), end()
#include <vector>
using namespace std;
member functions
int main() {
vector<int> intVec;
for (int i = 0; i < 5; i++) { intVec.push_back(i); }
intVec.push_back(-1);
for (vector<int>::iterator it = intVec.begin(); it != intVec.end(); it++) {
cout << *it << “ “;
}
return 0; 0 1 2 3 4 -1
}

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


Iterator Related Functions in “vector” Class
IIT Bombay

#include <iostream> rbegin(), rend()


#include <vector>
using namespace std;
member functions
int main() {
vector<int> intVec;
for (int i = 0; i < 5; i++) { intVec.push_back(i); }
intVec.push_back(-1);
for (vector<int>::reverse_iterator rit = intVec.rbegin(); rit != intVec.rend(); rit++) {
cout << *rit << “ “;
}
return 0;
}
-1 4 3 2 1 0

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


Inserting and Deleting Elements in the Middle
IIT Bombay

int main() {
vector<int> intVec; 01234
for (int i = 0; i < 5; i++) { intVec.push_back(i); }
for (int i = 0; i < 5; i++) { cout << intVec.at(i) << “ “;} cout << endl;
vector<int>::iterator it = intVec.begin() + 2;
Size after
intVec.insert(it, 0);
cout << “Size after insert: “ << intVec.size() << endl; insert: 6
for (int i = 0; i < intVec.size(); i++) { cout << intVec.at(i) << “ “;} cout << endl;
it = intVec.begin + 3; intVec.erase(it);
cout << “Size after delete: “ << intVec.size() << endl;
for (int i = 0; i < intVec.size(); i++) { cout << intVec.at(i) << “ “;} cout << endl;
return 0;
} 010234
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14
Inserting and Deleting Elements in the Middle
IIT Bombay

int main() {
vector<int> intVec;
for (int i = 0; i < 5; i++) { intVec.push_back(i); }
for (int i = 0; i < 5; i++) { cout << intVec.at(i) << “ “;} cout << endl;
vector<int>::iterator it = intVec.begin() + 2;
Size after
intVec.insert(it, 0);
cout << “Size after insert: “ << intVec.size() << endl; delete: 5
for (int i = 0; i < intVec.size(); i++) { cout << intVec.at(i) << “ “;} cout << endl;
it = intVec.begin + 3; intVec.erase(it);
cout << “Size after delete: “ << intVec.size() << endl;
for (int i = 0; i < intVec.size(); i++) { cout << intVec.at(i) << “ “;} cout << endl;
return 0;
} 01034
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15
Resizing a “vector”
IIT Bombay

• push_back(), pop_back result in automatic resizing


• C++ allows explicit resizing of vectors
int main() {
vector<int> intVec(5, 0);
Size after resizing: 10
intVec.resize(10, -1);
cout << “Size after resizing: “ << intVec.size() << endl;
for (int i = 0; i < 10; i++) { cout << intVec.at(i) << “ “; }
cout << endl;
intVec.resize(7); cout << “New size: “ << intVec.size() << endl;
for (int i = 0; i < 7; i++) { cout << intVec.at(i) << “ “; }
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16
Resizing a “vector”
IIT Bombay

• push_back(), pop_back result in automatic resizing


• C++ allows explicit resizing of vectors
int main() {
vector<int> intVec(5, 0);
0 0 0 0 0 -1 -1 -1 -1 -1
intVec.resize(10, -1);
cout << “Size after resizing: “ << intVec.size() << endl;
for (int i = 0; i < 10; i++) { cout << intVec.at(i) << “ “; }
cout << endl;
intVec.resize(7); cout << “New size: “ << intVec.size() << endl;
for (int i = 0; i < 7; i++) { cout << intVec.at(i) << “ “; }
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 17
Resizing a “vector”
IIT Bombay

• push_back(), pop_back result in automatic resizing


• C++ allows explicit resizing of vectors
int main() {
vector<int> intVec(5, 0);
New size: 7
intVec.resize(10, -1);
cout << “Size after resizing: “ << intVec.size() << endl;
for (int i = 0; i < 10; i++) { cout << intVec.at(i) << “ “; }
cout << endl;
intVec.resize(7); cout << “New size: “ << intVec.size() << endl;
for (int i = 0; i < 7; i++) { cout << intVec.at(i) << “ “; }
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 18
Resizing a “vector”
IIT Bombay

• push_back(), pop_back result in automatic resizing


• C++ allows explicit resizing of vectors
int main() {
vector<int> intVec(5, 0);
0 0 0 0 0 -1 -1
intVec.resize(10, -1);
cout << “Size after resizing: “ << intVec.size() << endl;
for (int i = 0; i < 10; i++) { cout << intVec.at(i) << “ “; }
cout << endl;
intVec.resize(7); cout << “New size: “ << intVec.size() << endl;
for (int i = 0; i < 7; i++) { cout << intVec.at(i) << “ “; }
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 19
Summary
IIT Bombay

• “vector” class and its usage


• Only some features studied
• We’ll study some more in next lecture

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

You might also like