473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Dec 5 '05 #1
5 2599
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
Dec 5 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4d*******************@newsread1.mlpsca01.us.t o.verio.net...
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::operator() ???

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::operator(const int& N, const int& M) {
return OtherClass[N] < OtherClass[M];
}
Dec 6 '05 #3
I corrected the example code below

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4d*******************@newsread1.mlpsca01.us.t o.verio.net...
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::operator() ???

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::operator(const int& N, const int& M) {
return OtherClass[N] < OtherClass[M];
}

Dec 6 '05 #4
Peter Olcott wrote:
I corrected the example code below

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4d*******************@newsread1.mlpsca01.us.t o.verio.net...
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::operator() ???

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::operator(const int& N, const int& M) {
Again, I'll assume

bool Two::operator()(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::operator()(const int& M, const int& M) {
// use 'other' -- it's a member!
}
};

V
Dec 6 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Cd******************************@comcast.com. ..
Peter Olcott wrote:
I corrected the example code below

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4d*******************@newsread1.mlpsca01.us.t o.verio.net...
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::operator() ???

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::operator(const int& N, const int& M) {


Again, I'll assume

bool Two::operator()(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.

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

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

V

Dec 6 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
3
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
3
by: BG | last post by:
Here's my program. { array<Byte> ^buffer = // something int length = Array::FindIndex(buffer, gcnew Predicate<Byte>(&MyClass::isChar13) ); ... } bool MyClass::isChar13(Byte b)
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.