sorting array and matching other array and getting error

I'm trying to write a program where I write information of people like: name,minutes and seconds.I need to sort people by the time. I can't understand how to sort minutes and move each person other information together- name and second. they just stay standing where they stood.Also I get error because something wrong with sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <algorithm>
#include <string>
using namespace std;

struct people{
string name;
int min;
int sec;
};
int main()
{
 int temp;
struct people z[6];
for(int i=0; i<6; i++)
    {
   cin>>z[i].name;
   cin>>z[i].min;
   cin>>z[i].sec;
 }
sort(z.min,z.min +6);
cout<<endl;

for(int i=0; i<6; i++)
    {
    cout<<z[i].name<<" "<<z[i].min<<" "<<z[i].sec<<endl;
 }
    return 0;
}


I am getting this error:
error: request for member 'min' in 'z', which is of non-class type 'people [6]'
Look at some sorting methods:

https://www.geeksforgeeks.org/bubble-sort/


You should have specified the line the error is on. It's on line 21 - you're trying to access the variable "min" in the array "z", but haven't specified which element in the array. Trying to access z.min is like trying to access ALL variable "min" within the array "z", which isn't something that's supported.

First, this:
struct people z[6];
No no no. That looks like C code. This is C++. Once you've defined a class, you just use it. Change like 14 to:
people z[6];


sort(z.min,z.min +6);

That's just plain not how sort work.

sort takes as parameters the range of items to be sorted. In this case,

sort(z,z +6);

If the items being sorted already have a comparison operator of form operator<, that will be used. If not, it is up to you to provide the function used to compare the items. Since you want to compare by people.min , you can write an operator to do that:

1
2
3
4
5
6
7
struct people
{
  string name;
  int min;
  int sec;
  bool operator< (const A& right) const { return min < right.min; }
}






Topic archived. No new replies allowed.