get the max "absolute" integer in a vector

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

    #1

    get the max "absolute" integer in a vector

    Hi,

    Can someone provide me an example that uses std::max_elemen t()
    (probablly the predicate version) to return the max "absolute" integer
    in a vector? Your help is much appreciated.

    Tony

    ps.

    // return the max integer in a vector
    std::vector<int m;
    ....
    std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end());
  • Piyo

    #2
    Re: get the max &quot;absolute& quot; integer in a vector

    JDT wrote:
    Hi,
    >
    Can someone provide me an example that uses std::max_elemen t()
    (probablly the predicate version) to return the max "absolute" integer
    in a vector? Your help is much appreciated.
    >
    Tony
    >
    ps.
    >
    // return the max integer in a vector
    std::vector<int m;
    ...
    std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end());
    This is the traditional way. You can also attemp to make ComparisonOp
    into a template if you wish.

    ------------------------------------------------
    #include <vector>
    #include <algorithm>
    #include <functional>

    class ComparisonOp : public std::binary_fun ction<bool, int, int>
    {
    public:
    result_type operator()( const first_argument_ type &a,
    const second_argument _type &b ) const
    {
    return (a < b);
    }
    };

    int
    main()
    {
    std::vector<int m;
    // fill m with elements
    std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
    ComparisonOp()) ;
    }
    ----------------------------------------------------------

    This uses boost lambda.
    ----------------------------------------------------------
    #include <vector>
    #include <algorithm>
    #include <boost/lambda/lambda.hpp>

    // this works if operator< is defined for your type (int in your case)

    using namespace boost::lambda;

    int
    main()
    {
    std::vector<int m;
    // fill m with elements
    std::vector<int >::iterator p =
    std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
    }

    -------------------------------------------------------------

    Comment

    • Piyo

      #3
      Re: get the max &quot;absolute& quot; integer in a vector

      Piyo wrote:
      JDT wrote:
      >Hi,
      >>
      >Can someone provide me an example that uses std::max_elemen t()
      >(probablly the predicate version) to return the max "absolute" integer
      >in a vector? Your help is much appreciated.
      >>
      >Tony
      >>
      >ps.
      >>
      >// return the max integer in a vector
      >std::vector<in tm;
      >...
      >std::vector<in t>::iterator p = std::max_elemen t(m.begin(), m.end());
      >
      This is the traditional way. You can also attemp to make ComparisonOp
      into a template if you wish.
      >
      ------------------------------------------------
      #include <vector>
      #include <algorithm>
      #include <functional>
      >
      class ComparisonOp : public std::binary_fun ction<bool, int, int>
      {
      public:
      result_type operator()( const first_argument_ type &a,
      const second_argument _type &b ) const
      {
      return (a < b);
      }
      };
      >
      int
      main()
      {
      std::vector<int m;
      // fill m with elements
      std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
      ComparisonOp()) ;
      }
      ----------------------------------------------------------
      >
      This uses boost lambda.
      ----------------------------------------------------------
      #include <vector>
      #include <algorithm>
      #include <boost/lambda/lambda.hpp>
      >
      // this works if operator< is defined for your type (int in your case)
      >
      using namespace boost::lambda;
      >
      int
      main()
      {
      std::vector<int m;
      // fill m with elements
      std::vector<int >::iterator p =
      std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
      }
      >
      -------------------------------------------------------------
      Oops I fogot to put the abs function/fabs if float/double
      in there but you get, the idea :)

      HTH

      Comment

      • David Harmon

        #4
        Re: get the max &quot;absolute& quot; integer in a vector

        On Wed, 14 Mar 2007 00:56:16 GMT in comp.lang.c++, JDT
        <jdt_young@yaho o.comwrote,
        >Hi,
        >
        >Can someone provide me an example that uses std::max_elemen t()
        >(probablly the predicate version) to return the max "absolute" integer
        >in a vector? Your help is much appreciated.
        >
        >Tony
        >
        >ps.
        >
        >// return the max integer in a vector
        >std::vector<in tm;
        >...
        >std::vector<in t>::iterator p = std::max_elemen t(m.begin(), m.end());
        I don't think I understand your question. Are you asking how to get
        from the iterator p to the vector element value?

        Comment

        • JDT

          #5
          Re: get the max &quot;absolute& quot; integer in a vector

          Hi Piyo,

          I totally understand your solution and appreciate your help. I wonder
          if there is a way to put different, existing STL algorithms together to
          achieve the same function as your class ComparisonOp? Any further help
          is appreciated.

          Tony

          Piyo wrote:
          Piyo wrote:
          >
          >JDT wrote:
          >>
          >>Hi,
          >>>
          >>Can someone provide me an example that uses std::max_elemen t()
          >>(probablly the predicate version) to return the max "absolute"
          >>integer in a vector? Your help is much appreciated.
          >>>
          >>Tony
          >>>
          >>ps.
          >>>
          >>// return the max integer in a vector
          >>std::vector<i ntm;
          >>...
          >>std::vector<i nt>::iterator p = std::max_elemen t(m.begin(), m.end());
          >>
          >>
          >This is the traditional way. You can also attemp to make ComparisonOp
          >into a template if you wish.
          >>
          >------------------------------------------------
          >#include <vector>
          >#include <algorithm>
          >#include <functional>
          >>
          >class ComparisonOp : public std::binary_fun ction<bool, int, int>
          >{
          >public:
          > result_type operator()( const first_argument_ type &a,
          > const second_argument _type &b ) const
          > {
          > return (a < b);
          > }
          >};
          >>
          >int
          >main()
          >{
          > std::vector<int m;
          > // fill m with elements
          > std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
          > ComparisonOp()) ;
          >}
          >----------------------------------------------------------
          >>
          >This uses boost lambda.
          >----------------------------------------------------------
          >#include <vector>
          >#include <algorithm>
          >#include <boost/lambda/lambda.hpp>
          >>
          >// this works if operator< is defined for your type (int in your case)
          >>
          >using namespace boost::lambda;
          >>
          >int
          >main()
          >{
          > std::vector<int m;
          > // fill m with elements
          > std::vector<int >::iterator p =
          > std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
          >}
          >>
          >-------------------------------------------------------------
          >
          Oops I fogot to put the abs function/fabs if float/double
          in there but you get, the idea :)
          >
          HTH

          Comment

          • JDT

            #6
            Re: get the max &quot;absolute& quot; integer in a vector

            David Harmon wrote:
            Hi David,

            That's not what I meant. What I need is a predicate that compares the
            absolute values of the vector elements. It's best that the predicate is
            composed of some existing STL algorithms, predicates and/or containers.
            Thanks.

            Tony

            On Wed, 14 Mar 2007 00:56:16 GMT in comp.lang.c++, JDT
            <jdt_young@yaho o.comwrote,
            >
            >>Hi,
            >>
            >>Can someone provide me an example that uses std::max_elemen t()
            >>(probablly the predicate version) to return the max "absolute" integer
            >>in a vector? Your help is much appreciated.
            >>
            >>Tony
            >>
            >>ps.
            >>
            >>// return the max integer in a vector
            >>std::vector<i ntm;
            >>...
            >>std::vector<i nt>::iterator p = std::max_elemen t(m.begin(), m.end());
            >
            >
            I don't think I understand your question. Are you asking how to get
            from the iterator p to the vector element value?
            >

            Comment

            • Piyo

              #7
              Re: get the max &quot;absolute& quot; integer in a vector

              JDT wrote:
              Hi Piyo,
              >
              I totally understand your solution and appreciate your help. I wonder
              if there is a way to put different, existing STL algorithms together to
              achieve the same function as your class ComparisonOp? Any further help
              is appreciated.
              >
              Tony
              >
              OH, Ok. Then get the min and max of the vector and then negate
              the min and max if any are negative and then get the max of those.
              Is that what you are talking about? Each step can be done with
              existing stl algorithms.

              Or were you thinking of something else?

              HTH

              Comment

              • Piyo

                #8
                Re: get the max &quot;absolute& quot; integer in a vector

                JDT wrote:
                Hi Piyo,
                >
                I totally understand your solution and appreciate your help. I wonder
                if there is a way to put different, existing STL algorithms together to
                achieve the same function as your class ComparisonOp? Any further help
                is appreciated.
                >
                Tony
                >
                Piyo wrote:
                >
                >Piyo wrote:
                >>
                >>JDT wrote:
                >>>
                >>>Hi,
                >>>>
                >>>Can someone provide me an example that uses std::max_elemen t()
                >>>(probablly the predicate version) to return the max "absolute"
                >>>integer in a vector? Your help is much appreciated.
                >>>>
                >>>Tony
                >>>>
                >>>ps.
                >>>>
                >>>// return the max integer in a vector
                >>>std::vector< intm;
                >>>...
                >>>std::vector< int>::iterator p = std::max_elemen t(m.begin(), m.end());
                >>>
                >>>
                >>This is the traditional way. You can also attemp to make ComparisonOp
                >>into a template if you wish.
                >>>
                >>------------------------------------------------
                >>#include <vector>
                >>#include <algorithm>
                >>#include <functional>
                >>>
                >>class ComparisonOp : public std::binary_fun ction<bool, int, int>
                >>{
                >>public:
                >> result_type operator()( const first_argument_ type &a,
                >> const second_argument _type &b ) const
                >> {
                >> return (a < b);
                >> }
                >>};
                >>>
                >>int
                >>main()
                >>{
                >> std::vector<int m;
                >> // fill m with elements
                >> std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
                >> ComparisonOp()) ;
                >>}
                >>----------------------------------------------------------
                >>>
                >>This uses boost lambda.
                >>----------------------------------------------------------
                >>#include <vector>
                >>#include <algorithm>
                >>#include <boost/lambda/lambda.hpp>
                >>>
                >>// this works if operator< is defined for your type (int in your case)
                >>>
                >>using namespace boost::lambda;
                >>>
                >>int
                >>main()
                >>{
                >> std::vector<int m;
                >> // fill m with elements
                >> std::vector<int >::iterator p =
                >> std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
                >>}
                >>>
                >>-------------------------------------------------------------
                >>
                >Oops I fogot to put the abs function/fabs if float/double
                >in there but you get, the idea :)
                >>
                >HTH
                or here is another idea:

                from <algorithm>
                std::max
                from <functional>
                std::less

                then
                std::max( -a, a ) gives the absolute value
                then less( max(-a,a), max(-b,b ) ) defines the less than
                operator of the absoulte value.

                Is this what you wanted?

                Comment

                • JDT

                  #9
                  Re: get the max &quot;absolute& quot; integer in a vector

                  Hi Piyo,

                  It's exactly what I meant. I am going to try your less( max(-a,a),
                  max(-b,b ) ) solution. Hope it will work. Thanks so much.

                  Tony


                  Piyo wrote:
                  JDT wrote:
                  >
                  >Hi Piyo,
                  >>
                  >I totally understand your solution and appreciate your help. I wonder
                  >if there is a way to put different, existing STL algorithms together
                  >to achieve the same function as your class ComparisonOp? Any further
                  >help is appreciated.
                  >>
                  >Tony
                  >>
                  >Piyo wrote:
                  >>
                  >>Piyo wrote:
                  >>>
                  >>>JDT wrote:
                  >>>>
                  >>>>Hi,
                  >>>>>
                  >>>>Can someone provide me an example that uses std::max_elemen t()
                  >>>>(probabll y the predicate version) to return the max "absolute"
                  >>>>integer in a vector? Your help is much appreciated.
                  >>>>>
                  >>>>Tony
                  >>>>>
                  >>>>ps.
                  >>>>>
                  >>>>// return the max integer in a vector
                  >>>>std::vector <intm;
                  >>>>...
                  >>>>std::vector <int>::iterat or p = std::max_elemen t(m.begin(), m.end());
                  >>>>
                  >>>>
                  >>>>
                  >>>This is the traditional way. You can also attemp to make ComparisonOp
                  >>>into a template if you wish.
                  >>>>
                  >>>------------------------------------------------
                  >>>#include <vector>
                  >>>#include <algorithm>
                  >>>#include <functional>
                  >>>>
                  >>>class ComparisonOp : public std::binary_fun ction<bool, int, int>
                  >>>{
                  >>>public:
                  >>> result_type operator()( const first_argument_ type &a,
                  >>> const second_argument _type &b ) const
                  >>> {
                  >>> return (a < b);
                  >>> }
                  >>>};
                  >>>>
                  >>>int
                  >>>main()
                  >>>{
                  >>> std::vector<int m;
                  >>> // fill m with elements
                  >>> std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
                  >>> ComparisonOp()) ;
                  >>>}
                  >>>----------------------------------------------------------
                  >>>>
                  >>>This uses boost lambda.
                  >>>----------------------------------------------------------
                  >>>#include <vector>
                  >>>#include <algorithm>
                  >>>#include <boost/lambda/lambda.hpp>
                  >>>>
                  >>>// this works if operator< is defined for your type (int in your case)
                  >>>>
                  >>>using namespace boost::lambda;
                  >>>>
                  >>>int
                  >>>main()
                  >>>{
                  >>> std::vector<int m;
                  >>> // fill m with elements
                  >>> std::vector<int >::iterator p =
                  >>> std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
                  >>>}
                  >>>>
                  >>>-------------------------------------------------------------
                  >>>
                  >>>
                  >>Oops I fogot to put the abs function/fabs if float/double
                  >>in there but you get, the idea :)
                  >>>
                  >>HTH
                  >
                  >
                  or here is another idea:
                  >
                  from <algorithm>
                  std::max
                  from <functional>
                  std::less
                  >
                  then
                  std::max( -a, a ) gives the absolute value
                  then less( max(-a,a), max(-b,b ) ) defines the less than
                  operator of the absoulte value.
                  >
                  Is this what you wanted?

                  Comment

                  • JDT

                    #10
                    Re: get the max &quot;absolute& quot; integer in a vector

                    Hi Piyo,

                    I tried your idea but didn't make it work. I am not familiar with STL
                    syntax. Could you kindly give me some hint? Thanks for your further help.

                    Tony

                    Piyo wrote:
                    JDT wrote:
                    >
                    >Hi Piyo,
                    >>
                    >I totally understand your solution and appreciate your help. I wonder
                    >if there is a way to put different, existing STL algorithms together
                    >to achieve the same function as your class ComparisonOp? Any further
                    >help is appreciated.
                    >>
                    >Tony
                    >>
                    >Piyo wrote:
                    >>
                    >>Piyo wrote:
                    >>>
                    >>>JDT wrote:
                    >>>>
                    >>>>Hi,
                    >>>>>
                    >>>>Can someone provide me an example that uses std::max_elemen t()
                    >>>>(probabll y the predicate version) to return the max "absolute"
                    >>>>integer in a vector? Your help is much appreciated.
                    >>>>>
                    >>>>Tony
                    >>>>>
                    >>>>ps.
                    >>>>>
                    >>>>// return the max integer in a vector
                    >>>>std::vector <intm;
                    >>>>...
                    >>>>std::vector <int>::iterat or p = std::max_elemen t(m.begin(), m.end());
                    >>>>
                    >>>>
                    >>>>
                    >>>This is the traditional way. You can also attemp to make ComparisonOp
                    >>>into a template if you wish.
                    >>>>
                    >>>------------------------------------------------
                    >>>#include <vector>
                    >>>#include <algorithm>
                    >>>#include <functional>
                    >>>>
                    >>>class ComparisonOp : public std::binary_fun ction<bool, int, int>
                    >>>{
                    >>>public:
                    >>> result_type operator()( const first_argument_ type &a,
                    >>> const second_argument _type &b ) const
                    >>> {
                    >>> return (a < b);
                    >>> }
                    >>>};
                    >>>>
                    >>>int
                    >>>main()
                    >>>{
                    >>> std::vector<int m;
                    >>> // fill m with elements
                    >>> std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
                    >>> ComparisonOp()) ;
                    >>>}
                    >>>----------------------------------------------------------
                    >>>>
                    >>>This uses boost lambda.
                    >>>----------------------------------------------------------
                    >>>#include <vector>
                    >>>#include <algorithm>
                    >>>#include <boost/lambda/lambda.hpp>
                    >>>>
                    >>>// this works if operator< is defined for your type (int in your case)
                    >>>>
                    >>>using namespace boost::lambda;
                    >>>>
                    >>>int
                    >>>main()
                    >>>{
                    >>> std::vector<int m;
                    >>> // fill m with elements
                    >>> std::vector<int >::iterator p =
                    >>> std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
                    >>>}
                    >>>>
                    >>>-------------------------------------------------------------
                    >>>
                    >>>
                    >>Oops I fogot to put the abs function/fabs if float/double
                    >>in there but you get, the idea :)
                    >>>
                    >>HTH
                    >
                    >
                    or here is another idea:
                    >
                    from <algorithm>
                    std::max
                    from <functional>
                    std::less
                    >
                    then
                    std::max( -a, a ) gives the absolute value
                    then less( max(-a,a), max(-b,b ) ) defines the less than
                    operator of the absoulte value.
                    >
                    Is this what you wanted?

                    Comment

                    • Piyo

                      #11
                      Re: get the max &quot;absolute& quot; integer in a vector

                      JDT wrote:
                      Hi Piyo,
                      >
                      I tried your idea but didn't make it work. I am not familiar with STL
                      syntax. Could you kindly give me some hint? Thanks for your further help.
                      >
                      Tony
                      >
                      >>
                      >from <algorithm>
                      >std::max
                      >from <functional>
                      >std::less
                      >>
                      >then
                      >std::max( -a, a ) gives the absolute value
                      >then less( max(-a,a), max(-b,b ) ) defines the less than
                      >operator of the absoulte value.
                      >>
                      >Is this what you wanted?
                      I would just stick it in a function
                      #include <algorithm>
                      #include <functional>
                      bool foo( int a, int b )
                      {
                      return std::less( std::max(-a,a), std::max(-b,b) );
                      }

                      // then
                      int
                      main()
                      {
                      std::vector<int m;
                      std::sort( m.begin(), m.end(), &foo );
                      }

                      Comment

                      Working...