473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

max_element with binary predicate

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<intv;
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;
}
Oct 26 '06 #1
2 2757
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

Oct 26 '06 #2
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
Oct 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
2
by: BCC | last post by:
What the heck am I doing wrong? void MyClass::JoinIdenticalFingerprints(CFPrintList& list) { CFPrintList unique_list = list; unique(unique_list.begin(), unique_list.end(), UniquePredicate); }...
1
by: Siemel Naran | last post by:
Hi. To find the maximum element in a container using the predicate form, do we supply a less predicate (one whose operator()(lhs, rhs) returns true if lhs < rhs), or a more predicate (one whose...
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
5
by: Peter Olcott | last post by:
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...
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
5
by: ma740988 | last post by:
Trying to determine how to get the max element from a complex sequence. Given: int main() { //typedef std::complex < double > C typedef std::vector < std::complex < double > > complex_vec;...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
5
by: Ganesh | last post by:
hi ! Can the predicate function used in std::sort take optional arguments ? For instance. I have a class Point. I create a vector of this and then want to compare the slopes of these points with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.