Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:54:30+00:00 2026-06-18T15:54:30+00:00

Yes, I know this is a repeat question and I already know that the

  • 0

Yes, I know this is a repeat question and I already know that the anwser im looking for is here:

Sorting a vector of objects by a property of the object

However I have problems converting this to my own code. I’m looking at this code snippet from the above question:

struct SortByX
{
    bool operator() const(MyClass const& L, MyClass const& R) {
        return L.x < R.x;
    }
};

std::sort(vec.begin(), vec.end(), SortByX();

What I do not understand is what is being represented by MyClass const & L, and MyClass const & R. And I am not grasping how I can apply this to my code.

To give a bit more detail I am putting 3 sort methods into a wrapper class of a vector of objects that have parameters of (string, double, double, double, bool). And the over all goal is to sort the vector by the string, the bool and any one out of the 3 doubles.

This is the lastest version I have:

void StationVector::sortByGrade(int kindOfGas) {
struct SortByGrade {
    int kindOfGas;

    SortByGrade(int kindOfGas) :
            kindOfGas(kindOfGas) {
    }

    bool operator()(GasStation const &L, GasStation const & R) const {
        return L.getPrice(kindOfGas) < R.getPrice(kindOfGas);
    }
};

std::sort(localStations.begin(), localStations.end(),
        SortByGrade(kindOfGas));
}

the line SortByGrade(kindOfGas)) gives me the following error:

no matching function for call to `sort(__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >, model::StationVector::sortByGrade(int)::SortByGrade)’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:54:31+00:00Added an answer on June 18, 2026 at 3:54 pm

    What I do not understand is what is being represented by MyClass const & L, and MyClass const & R.

    L and R, in this case are two items (instances of your class MyClass) from the container that are being compared, with L on the left of the less-than operator and R on the right. They are passed in by const-reference.

    And I am not grasping how I can apply this to my code.

    In your own bool operator() const(MyClass const& L, MyClass const& R), you need to compare the three data members you mention in your question, vitally remembering to apply strict weak ordering. Return true if L is “less than” R and false otherwise.


    Following updates to the question…

    It looks like you wish to pass a variable into your functor. You do this by creating a constructor, like this SSCCE (which compiles here):

    #include <algorithm>
    #include <vector>
    
    namespace model {
    
    struct GasStation
    {
        double getprice(int kindOfGas) const
        {
            return kindOfGas;
        }
    };
    
    struct StationVector
    {
        std::vector<GasStation> localStations;
    
        struct SortByGrade
        {
            int kindOfGas_;
            SortByGrade(int kindOfGas)
                :kindOfGas_(kindOfGas)
            {
            }
    
            bool operator() (GasStation const &L, GasStation const & R) const
            { 
                // You'll need other comparisons here, but this is a good start...
                return L.getprice(kindOfGas_) < R.getprice(kindOfGas_); 
            }
        };
    
        void sortByGrade(int kindOfGas) 
        {
            std::sort(localStations.begin(), localStations.end(), SortByGrade(kindOfGas));
        }
    };
    
    }
    
    int main()
    {
        model::StationVector sv;
        sv.sortByGrade(0);
    }
    

    Note: The const qualifier comes after the argument list and not after the method name.

    Also, please don’t put the entire method on one line, it makes it very difficult to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes, I know this question has been asked before, but I can't find an
Yes, I know this question has been asked a lot of times, but I
I know the Sales pitch answer is yes to this question, but is it
Yes, I know that the FAQ pretends to answer this, but it doesn't really.
Well yes i know this is an anecdotal question - and if you want
Yes I know, this title isn't really helpfull but this is the exact problem.
I can't find a straightforward yes or no answer to this! I know I
How can I convert this document: <Root> <!-- yes, I know I don't need
Yes I know that it shouldn't be abused and that C# is primariy used
(Yes I know I can call Java code from Scala; but that is pointless;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.