Showing posts with label GroupA_7. Show all posts
Showing posts with label GroupA_7. Show all posts

Wednesday, 24 August 2016

Create a class template to represent a generic vector. Include following member functions:  To create the vector.  To modify the value of a given element  To multiply by a scalar value  To display the vector in the form (10,20,30,…)

#include<iostream>
#include<vector>
using namespace std;
void display(vector<int> &v)
{
    for(int i=0;i<v.size();i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<"\n";
}

int main()
{
    vector<int> v;
    cout<<"\n initial size="<<v.size()<<" \n ";

    int x;
    cout<<"\n enter values ";
    for(int i=0;i<5;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    cout<<"\n\n size after inserting 5 values  "<<v.size();

    cout<<"\n\n current contents ";
    display(v);

    v.push_back(2);

    cout<<"\n\n size after push_back()= "<<v.size()<<"\n";
    cout<<"\n\n now the contents of vector after push_back()";
    display(v);

    cout<<"\n\n iterator created and inserted element at 3rd position ";
    vector<int>::iterator itr1=v.begin();
    itr1=itr1+2;
    v.insert(itr1,1,6);

    cout<<"\n\n contents of vector after insrting ";
    display(v);
    cout<<"\n\n removing elements 3rd and 4th position";
    //v.erase(v.begin()+2,v.begin()+6);
    v.erase(v.begin()+2,v.begin()+4);
    cout<<"\n\n contents after deletion ";
    display(v);
    cout<<"\n\n";
    return 0;

   }