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

Discussion in 'C++' started by Peter Olcott, Dec 5, 2005.

  1. Peter Olcott

    Peter Olcott Guest

    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
    Peter Olcott, Dec 5, 2005
    #1
    1. Advertising

  2. Re: Binary predicate member function needs access to another object'sdata

    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
    Victor Bazarov, Dec 5, 2005
    #2
    1. Advertising

  3. Peter Olcott

    Peter Olcott Guest

    "Victor Bazarov" <> wrote in message
    news:4d3lf.54481$01.us.to.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::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];
    }
    Peter Olcott, Dec 6, 2005
    #3
  4. Peter Olcott

    Peter Olcott Guest

    I corrected the example code below

    "Victor Bazarov" <> wrote in message
    news:4d3lf.54481$01.us.to.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::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];
    }
    Peter Olcott, Dec 6, 2005
    #4
  5. Peter Olcott wrote:
    > I corrected the example code below
    >
    > "Victor Bazarov" <> wrote in message
    > news:4d3lf.54481$01.us.to.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::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
    Victor Bazarov, Dec 6, 2005
    #5
  6. Peter Olcott

    Peter Olcott Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > Peter Olcott wrote:
    >> I corrected the example code below
    >>
    >> "Victor Bazarov" <> wrote in message
    >> news:4d3lf.54481$01.us.to.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::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.

    >
    > 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
    >
    Peter Olcott, Dec 6, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    594
    John Carson
    Jul 30, 2003
  2. BCC
    Replies:
    2
    Views:
    1,457
  3. DerekBaker

    Member function as predicate

    DerekBaker, Nov 3, 2007, in forum: C++
    Replies:
    1
    Views:
    318
    DerekBaker
    Nov 3, 2007
  4. Praetorian
    Replies:
    11
    Views:
    2,337
    James Kanze
    Apr 3, 2008
  5. blangela
    Replies:
    8
    Views:
    651
    Erik Wikström
    Sep 26, 2008
Loading...

Share This Page