Topic: Asymetric Types w/ Comparison Function Objects


Author: Sean Parent <sparent@adobe.com>
Date: Thu, 18 Apr 2002 19:17:28 GMT
Raw View
I was recently asked how to call lower_bound() on a vector that had been
sorted by a member. The problem being that creating a temporary object to
pass to lower_bound wasn't practical.

My first answer was to rewrite lower_bound() with a predicate (it's actually
bound_if() because it serves as both lower and upper bound). But then I
considered an asymmetric comparison function object. I can't find anything
in the specification that precludes this implementation (but also nothing
that implies it should work). At least with Metrowerks 7.1 it seems to work
just fine with lower_bound(), upper_bound(), and equal_range().

Is this covered at all by the standard? If this is conforming and defined
behavior than I'll go work on how to make something like boost::bind()
generate asymmetric function objects.

------

#include <iostream>
#include <algorithm>

typedef std::pair<int, double> pair_t;
struct less_t
{
    bool operator () (int x, const pair_t& y) const { return x < y.first; }
    bool operator () (const pair_t& x, int y) const { return x.first < y; }
};

int main()
{
    pair_t array[] = {
        pair_t(1, 10.0),
        pair_t(2, 20.0),
        pair_t(3, 30.0)
        };

    std::cout << std::lower_bound(&array[0], &array[3], 2, less_t())->second
<< std::endl;

    return 0;
}

------

Sean
--
Sean Parent
Sr. Computer Scientist II
Advanced Technology Group
Adobe Systems Incorporated
sparent@adobe.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Matt Austern <austern@apple.com>
Date: Thu, 18 Apr 2002 21:07:07 GMT
Raw View
In article <B8E46678.ECE5%sparent@adobe.com>,
 Sean Parent <sparent@adobe.com> wrote:

> I was recently asked how to call lower_bound() on a vector that had been
> sorted by a member. The problem being that creating a temporary object to
> pass to lower_bound wasn't practical.
>
> My first answer was to rewrite lower_bound() with a predicate (it's actually
> bound_if() because it serves as both lower and upper bound). But then I
> considered an asymmetric comparison function object. I can't find anything
> in the specification that precludes this implementation (but also nothing
> that implies it should work). At least with Metrowerks 7.1 it seems to work
> just fine with lower_bound(), upper_bound(), and equal_range().
>
> Is this covered at all by the standard? If this is conforming and defined
> behavior than I'll go work on how to make something like boost::bind()
> generate asymmetric function objects.

The short answer: No, it is not covered by the standard, because the
people who wrote this part of the standard didn't think of it in time.
HOWEVER,
 (a) It should work on all implementations unless the implementor goes
     out of their way to break it.
 (b) The standardization committee believes that it should work, and
     we have a tentative description of what it should mean.  A future
     version of the standard will almost certainly mandate that it works.
     See http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#270.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]