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

coder_newbie

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]

foragerDev

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is below program giving me error when I am assigning true or false to a vector of type bool?

I am trying to build my game in unity but it is giving me an error

I am trying to create an embed but it keeps giving me an invalid syntax with the variable name

Why does the program keeps giving me segmentation fault when I'm trying to make a user defined queue?

I am trying to alter a type in object relational model to add a member function to it, but it keeps giving me errors

i am getting an error while i am trying to add a user to a defined group while registration in django

I am trying to use amazon api code in php but it is giving me error of not supported version following is my code

I am trying to run a task in my async method but it's giving me a ui thread error

I am trying to use sharedPreferences from my Register Page and retrieve it on another class, but it is giving me error in the logcat

I am trying to update a value in an observable every second in setInterval but mobx is giving me error

I am trying to get the top 5 products using a Mysql Query but it's giving me an error

I am giving the generic type to the function parameter, when i call the function it gives me an error

i am trying to print words from a sentence in c++ but its giving me Segmentation Fault(core dump)

ACCESS keeping giving me a syntax error, when trying to create a view

Showing me a syntax error while I am trying to using the SELECT COUNT(*) function to count table rows with conditions?

I am trying to add data from word doc to access database but it give me operator missing syntax

Trying to generate email from data in Excel spreadsheet, getting compile error "User defined type not defined"

I am trying to use "sort" method but I am getting an error

I am trying to sort a certain range on the basis of two variables

I am trying to make a SQL query of Update in python but it is giving error

simpleLoginTool giving me "Error: The specified authentication type is not enabled for this Firebase" when I try to register new user

PyInstaller giving me a syntax error

I am getting a type error when I am trying to replace

Error creating the user defined data type

Why am I getting a syntax error when trying to create a table?

Why am I getting syntax error while trying to update database?

I am trying to extract certain values from JSON data with NodeJS

I am trying to access attribute which I passed but it's giving me a null pointer exception

Why am I getting Syntax error in "list.sort()"

TOP Ranking

HotTag

Archive