Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ sort coordinates stored as object in vector

In the next code I can input a number n then I can input n number of (x,y) coordinates.

Code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n = 0;
char cr;
class punto{
private:
    int x;
    int y;
public:
    punto();
    ~punto(){}
    void setx(int xn){ x = xn;}
    void sety(int yn) { y = yn; }
    void printcoord();
};
punto::punto()
{
    x=0;
    y=0;
}
void punto:: printcoord()
{
    cout << x << " " << y << endl;
}
int main()
{
    vector<punto> list;
    int x;
        int y;
        punto *p1;
    cin >> n >> cr;
    for(int contador=0; contador<n; contador++){
        if(contador<n){
                cin >> x;
                cin >> y;
                p1=new punto;
                p1->setx(x);
                p1->sety(y);
                list.push_back(*p1);
                cin.get();
            }
        }
    vector<punto>::iterator it;
    if(cr=='x'){
     for ( it = list.begin(); it != list.end(); ++it ) {
            it->printcoord();
     }
    }
    if(cr=='y'){
     for ( it = list.begin(); it != list.end(); ++it ) {
            it->printcoord();
     }
    }
    return 0;
}

That means that if we input this:

5 x
1 2
3 4
6 1
2 2
1 1

We get this output.

1 2
3 4
6 1
2 2
1 1

Problem is I don't know how to sort coordinates with respect to x or y.

Coordinates are stored as objects within a vector.

If I try to use sort it says that x and y are non-class type int.

Output should be:

1 1                    1 1
1 2                    6 1 
2 2     for x          1 2          for y    
3 4                    2 2 
6 1                    3 4

I would appreciate any help.

like image 439
Erick.1993 Avatar asked May 13 '26 01:05

Erick.1993


1 Answers

Output should be:

1 1                    1 1
1 2                    6 1 
2 2     for x          1 2          for y    
3 4                    2 2 
6 1                    3 4

So basically you wanna sort objects w.r.t either y or x. One simple way of doing it is defining operator< for your class punto

bool operator<(const punto& obj)
{
   return this->y < obj.y;   // this will help you to sort w.r.t Y
}

Now you can simply use std::sort(), how you normally use it.

std::sort(list.begin(), list.end());

In order to sort w.r.t xs, you can write a lambda function which will use getters(which you need to define in the class punto) of your class.

   // define getters to access the private memebers
   const int& getX()const { return x; }
   const int& getY()const { return y; }

Then you can do as follows:

   // to sort w.r.t Xs: std::sort() with lambda
   std::sort(list.begin(), list.end(),[](const punto& lhs, const punto& rhs)->bool
                                       {  return lhs.getX() < rhs.getX(); });

SEE OUTPUT HERE


However, I didn't understand why you have created punto *p1; and every time you have push_back the content to the vector.

One dangerous thing to note that, whatever you have created using new keyword, hasn't been deleted after, which is a serious memory leak issue in your problem.

You could have also use simply punto p1; or use Smart Pointers, if you really wanna play with dynamic memory.

like image 135
JeJo Avatar answered May 15 '26 15:05

JeJo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!