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

  • SEARCH
  • Home
  • 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 8094337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:46:08+00:00 2026-06-05T20:46:08+00:00

I have a class cl1: class c1 { long double * coords; … }

  • 0

I have a class cl1:

class c1
{
long double * coords;
...
}

I also have a second class cl2:

class cl2
{
vector<cl1*> cl1_vec;
unsigned int d;
...
}

I would like to sort cl1_vec, from cl2, based on coords[d], using the sort function for vectors.
So i could have something like

sort(cl2_inst->cl1_vec.begin(),cl2_inst->cl1_vec.end(), ??? );

I tried approches like

sort the 'std::vector' containing classes

C++ std::sort with predicate function in Class

but i couldn’t make my way to solving this.

Thanks for any help that comes this way.

The code i’ve tried:

class cl1 {
    public:
        long double* coords;

        cl1(long double *, unsigned int);
        cl1();
        cl1(const cl1& orig);
        virtual ~cl1();        
};

class cl2 {

    public:

    unsigned int d;

    vector<cl1*> cl1_vec;

    //the srting functions
    static bool compareMyDataPredicate(cl1* lhs, cl1* rhs)
    {
        return (lhs->coords[d] < rhs->coords[d]);
    };
    // declare the functor nested within MyData.
    struct compareMyDataFunctor : public binary_function<my_point*, my_point*, bool>
    {
        bool operator()( cl1* lhs, cl1* rhs)
        {
            return (lhs->coords[d] < rhs->coords[d]);
        }
    };
    ...
    ...
}

then in main

    std::sort(cl2_inst->cl1_vec.begin(),cl2_inst->cl1_vec.end(),cl2::compareMyDataPredicate() );
  • 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-05T20:46:10+00:00Added an answer on June 5, 2026 at 8:46 pm

    I am not sure about the problems, because you were not precise enough about what exactly “does not work” means in your case (does not compile, does compile but does not sort etc). In case it does not compile (most likely guess) you did not post the error messages, which also makes finding and explaining the problem very hard.

    Here are some guesses based on the code you posted:

    Both static function as well as the functor use the member d to decide which column to sort on. However d is an instance variable, so it is not available for anything which is static. Neither the functor nor the static member function would know which of the possible ds to use, as there is one d per instance.

    The best way to do this without resorting to C++11 features (lamdas) is to provide a constructor to the functor which takes the d you are planning to use. Something like this:

    struct compareMyDataFunctor : public binary_function<cl1*, cl1*, bool>
    {
        compareMyDataFunctor( unsigned int d ) : d( d ) {}
        bool operator()( cl1* lhs, cl1* rhs)
        {
            return (lhs->coords[d] < rhs->coords[d]);
        }
    
        unsigned int d;
    };
    

    This should do the trick.

    There are some more things wrong with the code you posted though:

    • Arrays should be indexed using type size_t not a unsigned int. Same goes for std::vectors
    • The types in the std::binary_function instantiation do not match the actual types in the method (may be a problem of reducing the code).
    • Do not use using namespace std (I assume you do from the declarations in your code).
    • Functors such as these should take parameters as const-reference, not by value.

    That’s all I can think of. Next time try to present short, self contained, complete examples, then people will not have to resort to guessing.

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

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have class A that has some primitive attributes and also member of type
Suppose I have a class library cl1 and a unit test for it that
I have class Distance and typedef enum Unit, @interface Distance:NSObject{ double m_miles; } @property
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have class with back reference: public class Employee : Entity { private string

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.