-1

[Error] 'class std::vector' has no member named 'sort' which sort can use in struct and how to use? sorry for my bad english.

typedef struct name
{
    int x,y;
}name;

int main()
{
    int n,m,i;
    vector<name> a(10000);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&m==0)
        {
            break;
        }   
        a.clear();
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].y=a[i].x%m;
        }
        a.sort();
        for(i=0;i<n;i++)
        {
            printf("%d\n",a[i].x);
        }
    }
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 4
    Some general notes about your code: In C++ you don't need `typedef` for structures or classes; Don't set the size of the vector, add elements to it as needed; Don't use anonymous one-letter variable names, use descriptive names; Write comments to tell what the code does, and why it does it that way; Learn how to use the C++ input/output streams `cin` and `cout`; And lastly, there's no vector `sort` member function, but there is a [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort) function which can sort any container. – Some programmer dude Apr 27 '20 at 15:12
  • 2
    You'll also need a way to compare two `name` objects. How do you know if one `name` is less than another `name`? – JohnFilleau Apr 27 '20 at 15:14
  • 2
    Oh and some extra notes: Split up the program into separate parts, preferably using functions;. For example one function to read input, then sort the vector (if it's really needed), then a function to do the processing, and lastly a function for the output.. Mixing input, algorithm and output makes the code harder to read, understand, maintain and debug. – Some programmer dude Apr 27 '20 at 15:15
  • Your question looks like a duplicate of https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects. Please search for existing questions before asking. – Xavier Lamorlette Apr 27 '20 at 21:16

2 Answers2

1

vector doesn't have a sort function. Instead, use std::sort, and implement an operator< for your struct.

struct name
{
    int x,y;
    bool operator<( const name &other ) const {
        return x < other.x; // modify to whatever sorting conditions you need
    }
};

Then for calling sort, do

std::sort( a.begin(), a.end() );
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • There is no need to alter the `struct`. The 3 argument version of `std::sort` is what would be used. – PaulMcKenzie Apr 27 '20 at 15:26
  • @PaulMcKenzie, depends on how often it'll be sorted. If it's sorted in more than one place, I prefer altering the struct. – ChrisMM Apr 27 '20 at 16:25
  • You could simply create a separate lambda function and use that. The point is that when you put an `operator <` inside a struct, it defines an attribute of that struct for everyone and everything. That operator has to make sense for all uses of that struct, and it won't make sense if the user wants to sort on the `y` coordinate, or maybe a sort on `x` and `y`. Also some structs just can't be adjusted or changed, and you're left with only dealing with the original struct. – PaulMcKenzie Apr 28 '20 at 01:42
1

Since std::vector has no sort() member function, you should use std::sort using the third argument as the sorting predicate. By doing so, you are not changing the internals of the struct you are sorting by adding an operator <:

#include <algorithm>
#include <vector>
struct name
{
    int x,y;
};

int main()
{
   std::vector<name> a(10000);

   // assuming you want to sort by x-coordinate  
   std::sort(a.begin(), a.end(), [](name& n1, name& n2) { return n1.x < n2.x; });
}

You can also create a separate lambda function:

#include <algorithm>
#include <vector>
struct name
{
    int x,y;
};

int main()
{
   std::vector<name> a(10000);
   auto comp = [](name& n1, name& n2) { return n1.x < n2.x; };

   // assuming you want to sort by x-coordinate  
   std::sort(a.begin(), a.end(), comp);
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45