-3
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct properties{
    int index;    // student's index number
    string name;  // name of student
    int points;   // points of exam

    bool sorter(properties a, properties b){
        return a.points < b.points;
    }

};

int main()
{
    properties students[6];

    vector<int> v;

    for(int i = 0; i < 6; i++){
        cin >> students[i].index >> students[i].name >> students[i].points;
    }

    for(int i = 0; i < 6; i++){
        v.push_back(students[i].points);
    }

    stable_sort(students.begin(), students.end(), sorter);

    return 0;
}

I have the following program and now I have to expand it to print the elements in sorted order from highest points to lowest. I need the smallest and most simple code because time isn't an issue in my case. Any help is appreciated.

UPDATE: I am getting two errors:

error: expected primary-expression before ',' token
error: expected primary-expression before '+' token

On this line:

sort(properties, properties + 5);
9
  • @DCoder I don't understand this example... sorry Commented Dec 25, 2013 at 12:53
  • 1
    have you tried std::sort? Commented Dec 25, 2013 at 12:54
  • @Erbureth Yes... but I don't know how to implement it with struct... I've used it before for arrays and it works perfectly, but I can't get it to work in this case.. Commented Dec 25, 2013 at 12:55
  • 3
    See the second form of std::sort, it allows you to specify your own comparator function. The documentation linked includes the example implementation. Commented Dec 25, 2013 at 12:56
  • 1
    You are not reading the documentation Commented Dec 25, 2013 at 13:01

2 Answers 2

1

I'll just give you some consecutive tips:

  1. Create std::vector and push you data into:

    vector<properties> students;
    
  2. Write the function which compares two structures and returns the comparison result. Don't make it a member of your structure. Notice that I've renamed it:

    bool less_than(properties const& first, properties const& second) 
    //my fault. Compiler is trying to call std::less<>() function instead of your.
    {  
      return first.points < second.points;    
    }  
    
  3. Call the std::sort function:

    sort(students.begin(), students.end(), less_than); 
    
  4. The data in your students structure will be sorted in the descending order.

This code works: http://ideone.com/iMWcfi

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I'll try right now. One more question. Will the other properties of the student be in same order? Like the name of the student who when sorted goes first stays the same?
@user2943407 see std::stable_sort
@Erbureth so It's the same with sort() except different name, however I have a question. When I use stable_sort() in main(), it says that less is not declared and 'request for member 'begin' and 'end' in 'students' is non-class type 'properties[6]'. When I declare the sorting inside the struct, then it says students was not declared. What am I doing wrong??
@user2943407 Keep updating the code in the question, so we can see what are you trying.
@user2943407 declare the sorter as static (static bool sorter(...){...}) and call it like sort(students, students + 6, properties::sorter);
|
0

See e.g. https://stackoverflow.com/a/10308722/1467943

bool operator <(const properties &a, const properties &b) {
  return a.points < b.points;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.