0

I was doing one programming problem where it is required to sort the class objects.

So, i used sort() to sort the array of objects but i couldn't do it, i read somewhere it requires lambda expression to that, but i don't know about them yet.

So, if you can help me with this question with some explanation of lambda expression it would be wonderful.

#include<bits/stdc++.h>
using namespace std;

class CallLog{
    public:
    string pno;
    int dur;
    string da;
    void setpno(string p_no) {pno = p_no;}
    void setdur(int d) {dur = d;}
    void setda(string dsa) {da = dsa;}
    string getpno() {return pno;}
    int getdur() {return dur;}
    string getda() {return da;}
};

int main() {
    int n;
    cin >> n;
    CallLog arr[n];
    for(int i = 0; i < n; i++) {
        string pno;
        int dur;
        string da;
        cin >> pno >> dur >> da;
        arr[i].setpno(pno);
        arr[i].setdur(dur);
        arr[i].setda(da);
    }
    sort(arr, arr+n, "what labmda expression to write?" );
    for(int i = 0; i < n; i++) {
        cout << "DialledNumber : " << arr[i].getpno() << ", Duration : " << arr[i].getdur() << ", Date : " << arr[i].getda() << "\n";
    }
}

I want to sort these objects with respect to the int dur.

Alex Ed
  • 41
  • 2
  • 9
  • 1
    **ProTip:** The variable-length arrays (VLAs) are not supported in C++. Therefore, statements like `cin >> n; CallLog arr[n];` – is invalid. Instead, use a `std::vector` or dynamic memory allocation with `new` and `delete` pair. – Rohan Bari Feb 25 '21 at 16:41
  • thanks @RohanBari for the Tip – Alex Ed Feb 25 '21 at 16:43

2 Answers2

1

Sorting is about comparing elements and knowing which one is "lower" than other one (ie, if I sort {5,3,1} into {1,3,5}, it is because 1 is lower than 3 and 3 is lower than 5)

So, it depends on what kind of logic you want to follow for your sorting algorithm (you could, for example, sort them by their member int dur). sort() uses the < operator to sort, so you have two options:

  1. Overload the < operator as shown here <= I suggest you to do this, for example:
bool operator<(CallLog a, CallLog b)
{
    return a.dur < b.dur;
}
  1. You can actually use a lambda, as in this example <= notice that even if it shows how to do it using lambdas, it also shows that writing the < operator would be way nicer. Also here, there's an easy-to-read example on how to sort passing a function to sort().
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ivan
  • 1,352
  • 2
  • 13
  • 31
  • Note that as mentioned by Rohan Bari , using `std::vector` is the way – Ivan Feb 25 '21 at 16:56
  • Thanks for the insights – Alex Ed Feb 25 '21 at 19:39
  • thank you sir, it was finally working as desired! – Alex Ed Feb 25 '21 at 20:12
  • 2
    The overloaded `operator<` should take its parameters by const reference, not by value: `bool operator<(const CallLog &a, const CallLog &b)` You can alternatively implement the `operator` as a member of `CallLog`, eg: `class CallLog { public: ... bool operator<(const CallLog &rhs) const { return dur < rhs.dur; } };` – Remy Lebeau Feb 25 '21 at 20:52
0

The predefined STL function objects, located in the FUNCTIONAL header file. There are function objects corresponding to all the major C++ operators. In the table, the letter T indicates any class, either user-written or a basic type. The variables x and y represent objects of class T passed to the function object as arguments. "bool = greater(T, T) x > y bool = less(T, T) x < y". Also if you want you can write your own function objects.