coder_newbie
coder_newbie

Reputation: 39

i am trying to sort a vector of certain user defined data type but it is giving me syntax error c++

While solving a certain ds algo question,I had to sort the array using a custom comparator.I don't know why is it showing me errors at that particular line

Code

 #include"bits/stdc++.h"
 using namespace std;
 struct train{
      
      int platform,arrive,depart;
 };
 bool compare(train x,train y)
 {
     
     return x.depart<y.depart;

 }
 int maxTrains(vector<train> arr[])
 {
     int ans=0;
     sort(arr,arr+arr.size(),compare);
     return ans;
 }
 int main()
 {
     return 0;
 }

Error message: no instance of overloaded function "sort" matches the argument list -- argument types are: (std::vector<train, std::allocator> *, , bool (train x, train y))[13,6]

{ "resource": "/F:/programming/dsa cracker sheet/maximum trains for which stoppage can be provided/max-trains.cpp", "owner": "C/C++", "code": "153", "severity": 8, "message": "expression must have class type but it has type "std::vector<train, std::allocator> *"", "source": "C/C++", "startLineNumber": 13, "startColumn": 19, "endLineNumber": 13, "endColumn": 22 }[13,19]

Upvotes: 0

Views: 503

Answers (3)

user2205930
user2205930

Reputation: 1086

Here's a cleaner approach. Your maxTrains function returns an int that is never set within the function. You simply need to sort the vector.

 #include <vector>
 #include <algorithm>
 using namespace std;
 
 struct train
 {
    int platform;
    int arrive;
    int depart;
 };
 
 bool compare(const train& x, const train& y)
 {
     return x.depart < y.depart;
 }
 
 void maxTrains(vector<train>& vec)
 {
     sort(vec.begin(), vec.end(), compare);
 }
 
 int main()
 {
     return 0;
 }

For compare be sure to pass by reference so you're not making copies of the object. The maxTrains functions needs the vector to be passed by reference so it can be modified, sorted, and returned. Also, be sure to include the correct header files.

Upvotes: 3

foragerDev
foragerDev

Reputation: 1409

There are several things wrong. First, This line is really saying vector<train>* arr, Pointer to a vector. But you used vector so array and memory management is totally internal to it. And you are using sort which is in-place sorting algorithms means, it modifies the original array, so you have to pass it via reference.

int maxTrains(vector<train> arr[])

This is the correct way.

int maxTrains(vector<train>& arr) //if you want modify original

Second, are you using sort which expects sort(Iterator begin, Iterator end, compare); This is how you call std::sort.

sort(arr.begin(), arr.end(),compare);

This is what you want, the rest works fine. I do not know what exactly you want to do with maxTrain so I am just fixing the errors.

 int maxTrains(vector<train>& arr)
 {
     int ans=0;
     sort(arr.begin(), arr.end(),compare);
     return ans;
 }

Using these lines is a very bad practice.

#include"bits/stdc++.h"
using namespace std;

Try to be explicitly as much as you can, this way you can learn more.

Upvotes: 1

Anubhav Gupta
Anubhav Gupta

Reputation: 431

Following changes will remove all compile time errors, however, logically your code will still have flaws.

 #include"bits/stdc++.h"
 using namespace std;
 struct train{
      
      int platform,arrive,depart;
 };
 bool compare(train x,train y)
 {
     
     return x.depart<y.depart;

 }
 int maxTrains(vector<train> arr) // change from array of vectors to single vector
 {
     int ans=0;
     sort(arr.begin(),arr.end(),compare); // use arr.begin() and arr.end()
     return ans;
 }
 int main()
 {
     return 0;
 }
  1. The vector you are sending to maxTrains is sent using pass-by-value, so any changes done by the function will not be reflected back in the calling function

  2. You are not calling maxTrains function

Upvotes: 1

Related Questions