Binary predicate member function needs access to another object's data

P

Peter Olcott

I created an object that requires access to another objects data, yet have found
no good way to pass this data as a parameter because the member function that
requires this data must be a binary predicate for std::sort. The only way that
I got it to work so far is to make the other object global. Are there any better
ways than this?

Thanks,


Peter Olcott
 
V

Victor Bazarov

Peter said:
I created an object that requires access to another objects data, yet have found
no good way to pass this data as a parameter because the member function that
requires this data must be a binary predicate for std::sort. The only way that
I got it to work so far is to make the other object global. Are there any better
ways than this?

Yes, create a sorting predicate, which would call your function, and give
that predicate object a data member, or simply use bind2nd (or bind1st) to
provide the argument to the predicate.

V
 
P

Peter Olcott

Victor Bazarov said:
Yes, create a sorting predicate, which would call your function, and give
that predicate object a data member, or simply use bind2nd (or bind1st) to
provide the argument to the predicate.

V

I tried to research this and it began to take too long. Below is code comparable
to my problem. What changes in syntax are you suggesting such that I can some
how pass OtherClass to Two::eek:perator() ???

Thanks

// assume operator<() defined for SomeType

class One {
std::vector<SomeType> Items;
One& operator[](const int N) { return Items[N] };
} OtherClass;


class Two {
std::vector<int> Subscripts;
void sort();
void operator(const int& N, const int& M));
};


void Two::sort() {
std::sort(SubScripts.begin(), SubScripts.end(), (*this));
}


void Two::eek:perator(const int& N, const int& M) {
return OtherClass[N] < OtherClass[M];
}
 
P

Peter Olcott

I corrected the example code below

Victor Bazarov said:
Yes, create a sorting predicate, which would call your function, and give
that predicate object a data member, or simply use bind2nd (or bind1st) to
provide the argument to the predicate.

V


I tried to research this and it began to take too long. Below is code comparable
to my problem. What changes in syntax are you suggesting such that I can some
how pass OtherClass to Two::eek:perator() ???

Thanks

// assume operator<() defined for SomeType

class One {
std::vector<SomeType> Items;
One& operator[](const int N) { return Items[N] };
} OtherClass;


class Two {
std::vector<int> Subscripts;
void sort();
bool operator(const int& N, const int& M);
};


void Two::sort() {
std::sort(SubScripts.begin(), SubScripts.end(), (*this));
}


bool Two::eek:perator(const int& N, const int& M) {
return OtherClass[N] < OtherClass[M];
}
 
V

Victor Bazarov

Peter said:
I corrected the example code below

Victor Bazarov said:
Yes, create a sorting predicate, which would call your function, and
give that predicate object a data member, or simply use bind2nd (or
bind1st) to provide the argument to the predicate.

V


I tried to research this and it began to take too long. Below is code
comparable to my problem. What changes in syntax are you suggesting
such that I can some how pass OtherClass to Two::eek:perator() ???

Thanks

// assume operator<() defined for SomeType

class One {
std::vector<SomeType> Items;
One& operator[](const int N) { return Items[N] };
} OtherClass;


class Two {
std::vector<int> Subscripts;
void sort();
bool operator(const int& N, const int& M);

Did you mean

bool operator()(const int& N, const int& M);

? I'll assume you did.
};


void Two::sort() {
std::sort(SubScripts.begin(), SubScripts.end(), (*this));
}


bool Two::eek:perator(const int& N, const int& M) {

Again, I'll assume

bool Two::eek:perator()(const int& N, const int& M) {
return OtherClass[N] < OtherClass[M];
}

OK, with two arguments already you won't be able to use 'bind2nd'. But
you definitely can construct your 'Two' object and pass your OtherClass
to it at the time:


class Two {
...
Other &other;
Two(One& o) : other(o) {}
...

bool Two::eek:perator()(const int& M, const int& M) {
// use 'other' -- it's a member!
}
};

V
 
P

Peter Olcott

Victor Bazarov said:
Peter said:
I corrected the example code below

Victor Bazarov said:
Peter Olcott wrote:
I created an object that requires access to another objects data,
yet have found no good way to pass this data as a parameter because
the member function that requires this data must be a binary
predicate for std::sort. The only way that I got it to work so far
is to make the other object global. Are there any better ways than
this?

Yes, create a sorting predicate, which would call your function, and
give that predicate object a data member, or simply use bind2nd (or
bind1st) to provide the argument to the predicate.

V


I tried to research this and it began to take too long. Below is code
comparable to my problem. What changes in syntax are you suggesting
such that I can some how pass OtherClass to Two::eek:perator() ???

Thanks

// assume operator<() defined for SomeType

class One {
std::vector<SomeType> Items;
One& operator[](const int N) { return Items[N] };
} OtherClass;


class Two {
std::vector<int> Subscripts;
void sort();
bool operator(const int& N, const int& M);

Did you mean

bool operator()(const int& N, const int& M);

? I'll assume you did.
};


void Two::sort() {
std::sort(SubScripts.begin(), SubScripts.end(), (*this));
}


bool Two::eek:perator(const int& N, const int& M) {

Again, I'll assume

bool Two::eek:perator()(const int& N, const int& M) {
return OtherClass[N] < OtherClass[M];
}

OK, with two arguments already you won't be able to use 'bind2nd'. But
you definitely can construct your 'Two' object and pass your OtherClass
to it at the time:
I can't construct the OtherClass because it already exists before class Two
exists, and it is full with many megabytes of data. If the reference syntax that
you are suggesting does not copy the data then it could work. I have not yet
used references in quite this way. I would guess that the syntax that you are
suggesting might not copy the data, and thus have possibly no overhead because
it might happen at compile-time instead of run-time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top