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;

   }


5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. ama bhai tme STL use karelu 6e...

    ReplyDelete
  3. it is told to define a class template, but you haven't done that.

    ReplyDelete
  4. MGM Resorts Casino to Open Casino In Reno - Dr.MD
    MGM Resorts 울산광역 출장안마 Casino, which opened its doors to the 전주 출장안마 general public in 제주도 출장마사지 2018, announced that it would 광양 출장샵 open its casino in 통영 출장안마 the city's Fremont

    ReplyDelete