-4
#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);
user2943407
  • 409
  • 3
  • 5
  • 13

2 Answers2

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

yakov
  • 444
  • 3
  • 14
  • 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 Dec 25 '13 at 13:27
  • @user2943407 see [`std::stable_sort`](http://en.cppreference.com/w/cpp/algorithm/stable_sort) – Erbureth Dec 25 '13 at 13:28
  • @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 Dec 25 '13 at 13:33
  • @user2943407 Keep updating the code in the question, so we can see what are you trying. – Erbureth Dec 25 '13 at 13:37
  • 1
    @user2943407 declare the `sorter` as `static` (`static bool sorter(...){...}`) and call it like `sort(students, students + 6, properties::sorter);` – Erbureth Dec 25 '13 at 13:44
  • @Erbureth Thanks a lot. I appreciate your help, you helped a LOT! Merry Christmas and happy holidays! – user2943407 Dec 25 '13 at 13:49
0

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

bool operator <(const properties &a, const properties &b) {
  return a.points < b.points;
}
Community
  • 1
  • 1
ales_t
  • 1,967
  • 11
  • 10