mercredi 1 juin 2016

call functions from vector of class in c++

I am writing a program for insertion sort.I am creating a class to read print and sort a vector of integers.I have created a vector of class and I want to call functions read,sort and print from vector of class created.How to do that ?

Thanks,

#include <iostream>
#include <vector> 

using namespace std;

class sorting
{  
    private:
    vector<int>arr;

    public:
    void read();
    void sortt();
    void print();
};

  void sorting :: read()
{
  int n;

 cin>>n;

 for(int i=0;i<n;i++)
    {
        int t;
        cin>>t;
        arr.push_back(t);
    }

}


void sorting :: sortt()
{
    int j,temp;

    for(unsigned int i=0;i<arr.size();i++)
        {
            temp=arr[i];
            j=i;

            while(temp<arr[j-1] && j>0)
                {
                    arr[j]=arr[j-1];
                    j=j-1;

                }
            arr[j]=temp;
        }
}

 void sorting :: print()
{
    for(unsigned int k=0;k<arr.size();k++)
        {
            cout<<arr[k]<<"\t";

        }
        cout<<endl;
        arr.clear();
}


int main()
{
 vector<sorting>s;

        s.read(); // giving an error
        s.sortt(); // giving an error

 return 0;
}

Aucun commentaire:

Enregistrer un commentaire