max_element with binary predicate

Discussion in 'C++' started by Mark P, Oct 27, 2006.

  1. Mark P

    Mark P Guest

    I'm trying to use max_element with a binary predicate. Here's an
    illustrative sample of the compilation problem I'm having. The compiler
    reports:

    closer.cpp:30: cannot convert `__gnu_cxx::__normal_iterator<int*,
    std::vector<int, std::allocator<int> > >' to `int' in initialization

    (line 30 is the penultimate statement of "main")

    Suggestions to fix this?

    Thanks,
    Mark

    // sample code

    #include <iostream>
    #include <algorithm>
    #include <vector>

    using namespace std;

    template <typename T>
    T absValue( T x) { return x < 0 ? -x : x; }

    template <typename T>
    struct CloserTo : public std::binary_function< T, T, bool>
    {
    CloserTo( T target) : target( target) {}
    bool operator()(T x, T y)
    { return absValue( x - target) < absValue( y - target); }

    T target;
    };

    int main()
    {
    vector<int> v;
    v.push_back(0);
    v.push_back(5);
    v.push_back(10);
    v.push_back(15);
    v.push_back(20);

    int target = 12;
    int idx = max_element( v.begin(), v.end(), CloserTo<int>( target));
    cout << v[idx] << endl;
    }
    Mark P, Oct 27, 2006
    #1
    1. Advertising

  2. Mark P

    Markus Moll Guest

    Hi

    Mark P wrote:

    > I'm trying to use max_element with a binary predicate. Here's an
    > illustrative sample of the compilation problem I'm having. The compiler
    > reports:
    >
    > closer.cpp:30: cannot convert `__gnu_cxx::__normal_iterator<int*,
    > std::vector<int, std::allocator<int> > >' to `int' in initialization

    [...]
    > int idx = max_element( v.begin(), v.end(), CloserTo<int>( target));


    Your problem is that max_element returns an iterator (to the maximum
    element) rather than the element itself.

    hth
    Markus
    Markus Moll, Oct 27, 2006
    #2
    1. Advertising

  3. Mark P

    Mark P Guest

    Markus Moll wrote:
    > Hi
    >
    > Mark P wrote:
    >
    >> I'm trying to use max_element with a binary predicate. Here's an
    >> illustrative sample of the compilation problem I'm having. The compiler
    >> reports:
    >>
    >> closer.cpp:30: cannot convert `__gnu_cxx::__normal_iterator<int*,
    >> std::vector<int, std::allocator<int> > >' to `int' in initialization

    > [...]
    >> int idx = max_element( v.begin(), v.end(), CloserTo<int>( target));

    >
    > Your problem is that max_element returns an iterator (to the maximum
    > element) rather than the element itself.
    >
    > hth
    > Markus
    >


    Oh, jeez, how embarrassing. Thank you!

    Mark
    Mark P, Oct 27, 2006
    #3
    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. BCC
    Replies:
    2
    Views:
    1,464
  2. Siemel Naran

    std::max_element

    Siemel Naran, Oct 20, 2004, in forum: C++
    Replies:
    1
    Views:
    2,418
    John Harrison
    Oct 20, 2004
  3. Peter Olcott
    Replies:
    5
    Views:
    374
    Peter Olcott
    Dec 6, 2005
  4. ma740988
    Replies:
    5
    Views:
    380
    ma740988
    May 25, 2006
  5. Stephen Howe

    max_element

    Stephen Howe, Oct 1, 2010, in forum: C++
    Replies:
    2
    Views:
    383
    Armen Tsirunyan
    Oct 1, 2010
Loading...

Share This Page