How to make a Sorting in classes?

Asked

Viewed 335 times

2

Funny thing is, that was an answer! The problem I have is this: I have a Person class, which initializes with a string, and it splits into a number and another string. After the class is mounted, I take an entire file and divide it into several strings, which will create several variables (objects). They should be drawn by number, and show everything on the screen. I set up the program but it shows nothing! My code is:

#include <iostream> //cout - main
#include <string> //string - main ,class and func
#include <algorithm> //reverse, sort, greater - class
#include <vector> //vector - main and func
#include <sstream> //stringstream - main

using namespace std;
class Person
{
    private:
        string name_;
        unsigned num_;
    public:
        Person(string raw)
        {
            this->num_ = stoi(raw);
            reverse(raw.begin(), raw.end()); //Those "reverse" are for getting only the number and the string
            raw = raw.substr(0, raw.find(' '));
            reverse(raw.begin(), raw.end()); //Ends here :P
            this->name_ = raw;
        }
        string name() {return this->name_;}
        unsigned num() {return this->num_;}
};

vector<Person> order_vector(vector<Person>& rawPerson)
{
        //Ok, lets check which one is the first
    vector<Person> ret;
    vector<unsigned> values;
    for(unsigned i = 0; i < rawPerson.size(); i++) values.push_back(rawPerson[i].num());
    sort(values.begin(), values.end(), greater<unsigned>());
    for(unsigned a = 0; a < rawPerson.size(); a++)
    for(unsigned i = 0; i < rawPerson.size(); i++)
    {
        if(a == rawPerson[i].num()) ret.push_back(rawPerson[i]);
    }
    return ret;
}

int main()
{
    string raw_strings;
    vector<Person> rawPerson;
    vector<Person> ordened_Person;
    stringstream rawstring(string("10 tennent\n9 Eccleston\n12 Capaldi\n11 Smith"));
    while(getline(rawstring, raw_strings)) //get info before '\n'
    {
        if(!raw_strings.empty()) //If it is not an '\n'
        {
            rawPerson.push_back(Person(raw_strings)); // add an object
        }
    }
    ordened_Person = order_vector(rawPerson);
    for(auto& a : ordened_Person) std::cout << a.num() << '\t' << a.name() << std::endl;
}

I believe the error is when returning the ordered vector, which is most likely. What I’m doing wrong?

1 answer

3


The good way to implement this is to make your class comparable, implement the operator<. Thus:

bool Person::operator<(const Person& other) {
    return num_ < other.num_;
}

Now you can order any container of people using the std::sort:

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

Or else:

std::sort(vecPerson.begin(), vecPerson.end(),
          [](const Person& p1, const Person& p2){ return p1.num() < p2.num(); });

Reinventing something the standard already does is rarely a good idea.

  • I was looking over the Sort now! Thank you :)

Browser other questions tagged

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