You are on page 1of 3

SSN College of Engineering, Kalavakkam – 603 110

Department of Information Technology

Subject Code: UEC2312 Staff Name: J. K. Josephine Julina


Subject Title: OOPS And Data Structures Lab Class: III SEM ECE
Batch: 2021 – 2025 Academic Year: 2022-2023
Vectors and Iterators
5. a. Write a program to get the following result.

Expected output for Vectors:

Enter the initial size: 5


Original Vector Contents: 0 1 2 3 4
Data at Position 4: 4
Push 12:
0 1 2 3 4 12
Insert -1 at the beginning
-1 0 1 2 3 4 12
Insert data 40 Index 4
-1 0 1 2 40 3 4 12
Erase data at position 2:
-1 0 2 40 3 4 12
Status 1 for empty is true ie. Empty, 0-empty is false ie. Not empty
0
After using clear command:
1

//5. a. STL - Standard template library


//used to store and process data
//library of container classes, algorithms and iterators
//vectors are part of STL
//vectors are dynamic arrays
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vin;
int i,j,limit;
cout<<"Enter the initial size: ";
cin>>limit;
for(i=0; i<limit;i++)
vin.push_back(i);
cout<<"Original Vector Contents: ";
for(j=0;j<vin.size();j++)
cout<<vin[j]<<"\t";
cout<<"\nData at Position 4: "<<vin.at(4)<<"\n";
cout<<"Push 12: \n";
vin.push_back(12);
for(j=0;j<vin.size();j++)

Prepared by,
J. K. Josephine Julina
SSN College of Engineering, Kalavakkam – 603 110
Department of Information Technology

cout<<vin[j]<<"\t";
cout<<"\nInsert -1 at the beginning"<<"\n";
vin.insert(vin.begin(),-1);
for(j=0;j<vin.size();j++)
cout<<vin[j]<<"\t";
cout<<"\nInsert data 40 Index 4"<<"\n";
vin.insert(vin.begin()+4,40);
for(j=0;j<vin.size();j++)
cout<<vin[j]<<"\t";
cout<<"\nErase data at position 2: "<<"\n";
vin.erase(vin.begin()+2);
for(j=0;j<vin.size();j++)
cout<<vin[j]<<"\t";
cout<<"\nStatus 1 for empty is true ie. Empty, 0-empty is false ie. Not empty";
cout<<"\n"<<vin.empty();
cout<<"\nAfter using clear command: ";
vin.clear();
cout<<"\n"<<vin.empty();
return 0;
}

5. b. Write a program to get the following result.

Expected output using Iterators:

10 50

5 10 50 100

100

100

50

#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vin;
vin.push_back(10);
vin.push_back(50);
for(int i=0; i<vin.size(); i++)
cout<<vin.at(i)<<"\t";
vin.insert(vin.begin(),5);
vin.push_back(100);
cout<<"\n";
vector<int>::iterator it;
for (it=vin.begin(); it<vin.end(); it++)

Prepared by,
J. K. Josephine Julina
SSN College of Engineering, Kalavakkam – 603 110
Department of Information Technology

cout<<*it<<"\t";
it=vin.begin();
advance(it, 3);
cout<<"\n"<<*it<<"\n";
vector<int>::iterator btr = vin.begin();
vector<int>::iterator etr = vin.end();
auto ait = next(btr, 3);
auto ait1 = prev(etr, 2);
cout << *ait <<"\n";
cout << *ait1 <<"\n ";
return 0;
}

5. c Write a program to find the product of two matrices using vectors and iterators.

Try it!

Prepared by,
J. K. Josephine Julina

You might also like