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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Olcott

    #1

    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


  • Victor Bazarov

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

    Peter Olcott wrote:[color=blue]
    > 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?[/color]

    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

    Comment

    • Peter Olcott

      #3
      Re: Binary predicate member function needs access to another object's data


      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
      news:4d3lf.5448 1$Tf5.32046@new sread1.mlpsca01 .us.to.verio.ne t...[color=blue]
      > Peter Olcott wrote:[color=green]
      >> 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?[/color]
      >
      > 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[/color]

      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<Som eType> 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(SubSc ripts.begin(), SubScripts.end( ), (*this));
      }


      void Two::operator(c onst int& N, const int& M) {
      return OtherClass[N] < OtherClass[M];
      }


      Comment

      • Peter Olcott

        #4
        Re: Binary predicate member function needs access to another object's data

        I corrected the example code below

        "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
        news:4d3lf.5448 1$Tf5.32046@new sread1.mlpsca01 .us.to.verio.ne t...[color=blue]
        > Peter Olcott wrote:[color=green]
        >> 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?[/color]
        >
        > 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[/color]


        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<Som eType> 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(SubSc ripts.begin(), SubScripts.end( ), (*this));
        }


        bool Two::operator(c onst int& N, const int& M) {
        return OtherClass[N] < OtherClass[M];
        }



        Comment

        • Victor Bazarov

          #5
          Re: Binary predicate member function needs access to another object's data

          Peter Olcott wrote:[color=blue]
          > I corrected the example code below
          >
          > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
          > news:4d3lf.5448 1$Tf5.32046@new sread1.mlpsca01 .us.to.verio.ne t...[color=green]
          >> Peter Olcott wrote:[color=darkred]
          >>> 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?[/color]
          >>
          >> 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[/color]
          >
          >
          > 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<Som eType> 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);[/color]

          Did you mean

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

          ? I'll assume you did.
          [color=blue]
          > };
          >
          >
          > void Two::sort() {
          > std::sort(SubSc ripts.begin(), SubScripts.end( ), (*this));
          > }
          >
          >
          > bool Two::operator(c onst int& N, const int& M) {[/color]

          Again, I'll assume

          bool Two::operator() (const int& N, const int& M) {
          [color=blue]
          > return OtherClass[N] < OtherClass[M];
          > }[/color]

          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


          Comment

          • Peter Olcott

            #6
            Re: Binary predicate member function needs access to another object's data


            "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
            news:Cd-dnW7F9cSgmgjenZ 2dnUVZ_t6dnZ2d@ comcast.com...[color=blue]
            > Peter Olcott wrote:[color=green]
            >> I corrected the example code below
            >>
            >> "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
            >> news:4d3lf.5448 1$Tf5.32046@new sread1.mlpsca01 .us.to.verio.ne t...[color=darkred]
            >>> 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[/color]
            >>
            >>
            >> 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<Som eType> 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);[/color]
            >
            > Did you mean
            >
            > bool operator()(cons t int& N, const int& M);
            >
            > ? I'll assume you did.
            >[color=green]
            >> };
            >>
            >>
            >> void Two::sort() {
            >> std::sort(SubSc ripts.begin(), SubScripts.end( ), (*this));
            >> }
            >>
            >>
            >> bool Two::operator(c onst int& N, const int& M) {[/color]
            >
            > Again, I'll assume
            >
            > bool Two::operator() (const int& N, const int& M) {
            >[color=green]
            >> return OtherClass[N] < OtherClass[M];
            >> }[/color]
            >
            > 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:
            >[/color]
            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.
            [color=blue]
            >
            > 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
            >[/color]


            Comment

            Working...