I want to sort the double pointers in std::sort

Asked 1 years ago, Updated 1 years ago, 327 views

Questions about std::sort.I'd like to sort the double pointers in alphabetical order.To that end, we created the following program.However, it seems that the comparison() is not working well.If you know how to change it, please let me know.

I think the problem is with comparison().If you run this program, an error will get an error.The error is similar to the following:
error C2664: 'bool(Student&, Student&)': Argument 2 cannot be converted from 'Student*' to 'Student&'.
I think the conversion is not going well.
Also, I changed sort(list, list+size, comparison); to sort(*list), (*list+size), comparison; and the compilation was completed, but when I finished putting in the size information, a bug occurred and it stopped.
If there is anyone who can solve this problem based on this information, please take care of it.
I'm using the pointer with my class, so just in case, I've included information about the class.If anyone understands, please take care of me.

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

class Student
{
private:
    US>string name;
    intage;
public:
    Student() {name=";age=0;}
    Student(string str, int num) {name=str;age=num;}

    string getName()const {return name;}
    int getAge() const {returnage;}
};

Student* createStudent()
{
    US>string name;
    intage;


    cout<<"Enter name and age"<<endl;
    cin>>name;
    cin>>age;
    Student* student=new Student(name,age);


    return student;
}

void display(Student**constlist, int size)
{
    for (inti=0; i<size;i++)
    {
        cout<<list[i]->getName()<":"<<list[i]->getAge()<endl;
    }
}

Boolean comparison (Student&list1, Student&list2)
{
    return list1.getName()<list2.getName();
}

int main()
{

    int size = 3;

    Student**list=new Student* [size];

    for (inti=0; i<size;i++)
    {
        list[i] = createStudent();
    }
    sort(list, list+size, comparison);
    display(list,size);

    return 0;
}

c++

2022-09-30 21:56

2 Answers

The sort comparison function specifies that the argument receives two elements in the array.
Student**list is in the form of a double pointer, but it is actually an array of elements of type Student*.
Therefore, the comparison function must be Student* as the argument.

comparison and

bool comparison(Student*a, Student*b){
    /**/
}

Make sure that

[By the way]
The comparison function can also receive elements of an array as references.If the element type of the array is Student, it is and the presentation code is comparison.You may have been confused with this case.

If the comparison function receives a value rather than a reference type or pointer, each time an element is compared during sorting, the value is copied and the process can be slow.To avoid this, class or structure is often passed by reference.


2022-09-30 21:56

The error message means that the comparison() argument must be of type Student* instead of Student&.This cannot be changed because the sort is an array of Student*.


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.