0
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Cords
{
public:
int x,y,z;
};

class Object:public Cords
{
int a,b,c;
public:
vector<Cords> vCords;
void insertCords();
void getCords();
};

void Object::insertCords()
{
Cords theCords;
int x=1;
do
{
cout<<"Enter X:"<<endl;
cin>>x;
cout<<"Enter a b c:"<<endl;
cin>>a>>b>>c;
theCords.x=a;
theCords.y=b;
theCords.z=c;
vCords.push_back(theCords);
}while(x!=0);
}

void Object::getCords()
{
cout<<"ORIGINAL VALUES BEFORE SORTING:"<<endl;
for(int i=0;i<vCords.size();i++)
cout<<vCords[i].x<<" "<<vCords[i].y<<" "<<vCords[i].z<<endl;
**sort(vCords.begin(),vCords.end());**
}

int main()
{
Object obj;
obj.insertCords();
obj.getCords();
return 0;
}

HOw to use sort function for vector vCords. It din't work with algorithm sort(). Is there other possibility of sorting these types of vectors

2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by deceptikon
1

By default std::sort() uses operator< to handle the relation between items. Since you have neither an overloaded operator< nor specify a custom comparison function, of course it doesn't work.

Consider using the three argument version of std::sort(). You can find details and examples here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.