Sorting of object vectors

Asked

Viewed 844 times

6

I am making an application that sorts an object vector. These objects are Cartesian points, defined in the following class:

class CartesianPoint {
private:
    int x; // Coordinate X of a pin or a wire part on a cartesian plan
    int y; // Coordinate Y of a pin or a wire part on a cartesian plan

public:
    CartesianPoint();
    CartesianPoint(const CartesianPoint& orig);
    virtual ~CartesianPoint();

    int getX() const {
        return x;
    }

    int getY() const {
        return y;
    }

    CartesianPoint(int x, int y) :
    x(x), y(y) {
    }

To sort, I am using the Sort function as follows:

int main() 
{
    int i;
    CartesianPoint *a = new CartesianPoint(10, 13), *b = new CartesianPoint(32, 1), *c = new CartesianPoint(5, 29), *d = new CartesianPoint(12, 6), *e = new CartesianPoint(21, 100);
    CartesianPoint myPoints[] = {*a, *b, *c, *d, *e};

    std::vector<CartesianPoint> myvector(myPoints, myPoints+8);        

    // using function as comp
    std::sort(myvector[0], myvector.end(), myfunction); 

    // print out content:
    std::cout << "myvector contains:";
    for (i = 0; i < 8; i++)
        std::cout << ' ' << '(' << myvector[i].getX() << ',' << myvector[i].getY() << ')';
    std::cout << '\n';

    return 0;
}

where "myfunction" is the function that sorts points decreasing from x:

CartesianPoint* myfunction(CartesianPoint *a, CartesianPoint *b) {
    if(a->getX() > b->getX())
        return (a);
    else
        return (b);
}

The problem you’re giving is in the function call sort:

sort.cpp:33:5: error: no matching function for call to 'sort'
    std::sort(myvector[0], myvector.end()); 

What is may be wrong?

  • Where are the includes? Edit your code here and show us, please!

3 answers

7


Answering the question:

The problem that is giving is in the call of the function Sort:

The function sort is the type Comparable and must return a value of type bool (and not a pointer to the class Cartesianpoint).


There are still other problems in the program, such as the reference array, which can be replaced by an array of pointers.

Follows a functional version with few modifications:

class CartesianPoint {
private:
    int x; // Coordinate X of a pin or a wire part on a cartesian plan
    int y; // Coordinate Y of a pin or a wire part on a cartesian plan

public:
    CartesianPoint() {}

    CartesianPoint(const CartesianPoint& orig)
    {}

    CartesianPoint(int x, int y) : x(x), y(y)
    {
    }

    virtual ~CartesianPoint()
    {}

    int getX() const {
        return x;
    }

    int getY() const {
        return y;
    }

};

// Aqui => a função retorna um boolean
bool myfunction(CartesianPoint *a, CartesianPoint *b) {
    return a->getX() < b->getX();
}

int main()
{
    int i;
    CartesianPoint *a = new CartesianPoint(10, 13), 
        *b = new CartesianPoint(32, 1), 
        *c = new CartesianPoint(5, 29), 
        *d = new CartesianPoint(12, 6), 
        *e = new CartesianPoint(21, 100);

    // Aqui => trocado para ponteiros e não referências!!
    CartesianPoint *myPoints[] = { a, b, c, d, e };

    // O vetor é de ponteiros!
    std::vector<CartesianPoint *> myvector(myPoints, myPoints + sizeof(myPoints) / sizeof(myPoints[0]));

    // using function as comp
    // O primeiro parâmetro é um iterator => begin()
    std::sort(myvector.begin(), myvector.end(), myfunction);

    // print out content:
    std::cout << "myvector contains:";
    for (i = 0; i < 5; i++)
        std::cout << ' ' << '(' << myvector[i]->getX() << ',' << myvector[i]->getY() << ')';
    std::cout << '\n';

    return 0;
}

5

Well, by doing a little research here, I’ve seen that there have been several of these problems and their answers right here in the O.R., the most complete one I’ve found is this one:

https://stackoverflow.com/questions/30814618/no-matching-function-for-call-to-sort

The problem is very simple. The function you chose to use is this:

void sort( RandomIt first, RandomIt last, Compare comp );

But these variables first and last await Iterators to go through your collection, since this function is implemented as a template to accept various types of collections. Your lastAll right, only the first is wrong, you did not pass the iterator but the element. Correct and put the following:

std::sort(myvector.begin(), myvector.end(), myfunction); 

This is in the documentation itself: http://www.cplusplus.com/reference/algorithm/sort/

And also, its function myfunction returns a pointer to its object, which has to return a value of type bool, because it’s a comparison function. You should fix it too:

bool myfunction(CartesianPoint &a, CartesianPoint &b)
{
   return a.getX() < b.getX();
}

3

Looks like a program conversion from C# to C++...

Below a corrected C++11 version, removing the C#ismos:

#include <vector>
using std::vector;

#include <algorithm>
using std::sort;

#include <iostream>
using std::cout;

class CartesianPoint
{
   private:
      int x;
      int y;

   public:
      int getX() const { return x; }
      int getY() const { return y; }
      CartesianPoint(int x, int y) : x(x), y(y) { }
};

static bool myfunction(const CartesianPoint& a, const CartesianPoint& b)
{
   return a.getX() < b.getX();
}

int main() 
{
   vector<CartesianPoint> myvector { {10, 13}, {32, 1}, {5, 29}, {12, 6}, {21, 100} };

   sort(myvector.begin(), myvector.end(), myfunction); 

   cout << "myvector contains:";

   for (const auto& point : myvector)
       cout << ' ' << '(' << point.getX() << ',' << point.getY() << ')';

   cout << '\n';
}

ideone.: http://ideone.com/wDSIV0

Below a version for C++14:

#include <vector>
using std::vector;

#include <algorithm>
using std::sort;

#include <iostream>
using std::cout;

class CartesianPoint
{
   private:
      int x;
      int y;

   public:
      int getX() const { return x; }
      int getY() const { return y; }
      CartesianPoint(int x, int y) : x(x), y(y) { }
      bool operator< (const CartesianPoint& that) const { return x < that.x; }

};

int main() 
{
   vector<CartesianPoint> myvector { {10, 13}, {32, 1}, {5, 29}, {12, 6}, {21, 100} };

   auto cmpFunc = [](const auto& p1, const auto& p2) { return p1 < p2; };
   sort(myvector.begin(), myvector.end(), cmpFunc); 

   cout << "myvector contains:";

   for (const auto& point : myvector)
       cout << ' ' << '(' << point.getX() << ',' << point.getY() << ')';

   cout << '\n';
}

ideone.: http://ideone.com/v3fSdV

That’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.