PROGRAM:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> myarr;
cout << "\nSize : " << myarr.size() << endl;
//put data at vector
myarr.push_back(5);
myarr.push_back(10);
myarr.push_back(15);
myarr.push_back(20);
myarr.push_back(25);
cout << "\nSize : " << myarr.size() << endl;
for (int i = 0; i < myarr.size(); i++)
cout << "\tArray values " << myarr[i] << endl;
//delete first value
myarr.erase(myarr.begin());
cout << "\nSize : " << myarr.size() << endl;
for (int i = 0; i < myarr.size(); i++)
cout << "\tArray values " << myarr[i] << endl;
//delete last value
myarr.erase(myarr.end()-1);
cout << "\nSize : " << myarr.size() << endl;
for (int i = 0; i < myarr.size(); i++)
cout << "\tArray values " << myarr[i] << endl;
//insert at position
myarr.insert(myarr.begin()+2, 100);
cout << "\nSize : " << myarr.size() << endl;
for (int i = 0; i < myarr.size(); i++)
cout << "\tArray values " << myarr[i] << endl;
//delete middle value
myarr.erase(myarr.begin()+1);
cout << "\nSize : " << myarr.size() << endl;
for (int i = 0; i < myarr.size(); i++)
cout << "\tArray values " << myarr[i] << endl;
return 0;
}
OUTPUT:
Size : 0
Size : 5
Array values 5
Array values 10
Array values 15
Array values 20
Array values 25
Size : 4
Array values 10
Array values 15
Array values 20
Array values 25
Size : 3
Array values 10
Array values 15
Array values 20
Size : 4
Array values 10
Array values 15
Array values 100
Array values 20
Size : 3
Array values 10
Array values 100
Array values 20
No comments:
Post a Comment