Finding the left-most and right-most points of a list. std::find_if the right way to go?

I have a list of Point objects, (each one with x,y properties) and would like to find the left-most and right-most points. I've been trying to do it with find_if, but i'm not sure its the way to go, because i can't seem to pass a comparator instance. Is find_if the way to go? Seems not. So, is there an algorithm in <algorithm> to achieve this?

Thanks in advance.

#include <iostream> #include <list> #include <algorithm> using namespace std; typedef struct Point{ float x; float y; } Point; bool left(Point& p1,Point& p2) { return p1.x < p2.x; } int main(){ Point p1 ={-1,0}; Point p2 ={1,0}; Point p3 ={5,0}; Point p4 ={7,0}; list <Point> points; points.push_back(p1); points.push_back(p2); points.push_back(p3); points.push_back(p4); //Should return an interator to p1. find_if(points.begin(),points.end(),left); return 0; }

-------------Problems Reply------------

Use std::min_element and std::max_element instead.

list<Point>::iterator left = std::min_element(points.begin(), points.end(), left);
list<Point>::iterator right = std::max_element(points.begin(), points.end(), left);

I would also change the signature of left to:

bool left(const Point& p1, const Point& p2)

even better would be to use the boost minmax element:

#include <boost/algorithm/minmax_element.hpp>
auto res = boost::minmax_element(points.begin(), points.end(), left);

std::cout << "min: " << res.first << std::endl;
std::cout << "max: " << res.second << std::endl;

If you use pair<float, float> instead of your own Point, there's no need for a special comparator. There would also be an ordering on the y-axis of points with the same x-coordinate, which can be useful.

There are various ways to imbue typedef pair<float, float> Point; with custom behavior, if you're so inclined. For example,

typedef pair<float, float> Point;

enum AxisUnit { x, y };
float &operator*( Point &p, AxisUnit c ) // "special case" of inner product
{ return c == x? p.first : p.second; }

Point my_point( 2.5, 6.3 );
float x_coord = my_point * x;

Category:c# Views:0 Time:2010-04-16

Related post

  • Thread safe lazy construction of a singleton in C++ 2008-08-09

    Is there a way to implement a singleton object in C++ that is: Lazily constructed in a thread safe manner (two threads might simultaneously be the first user of the singleton - it should still only be constructed once). Doesn't rely on static variabl

  • How do you open a file in C++? 2008-08-11

    I want to open a file for reading, the C++ way. I need to be able to do it for text files, which would involve some sort of read line function, and a way to do it for binary files, which would provide a way to read raw data into a char* buffer. -----

  • How do you determine the size of a file in C? 2008-08-11

    How can I figure out the size of a file, in bytes? #include <stdio.h> unsigned int fsize(char* file){ //what goes here? } --------------Solutions------------- Based on NilObject's code: #include <sys/stat.h> off_t fsize(const char *filena

  • Secure Memory Allocator in C++ 2008-08-12

    I want to create an allocator which provides memory with the following attributes: cannot be paged to disk. is incredibly hard to access through an attached debugger The idea is that this will contain sensitive information (like licence information)

  • How to use xpath in Python? 2008-08-12

    Is there a full implementation? How is the library used, where is its website? --------------Solutions------------- libxml2 has a number of advantages: Compliance to the spec Active development and a community participation Speed. This is really a py

  • Should I move from C++ to Python? - Or another language? 2008-08-12

    In the company I work for, we do a lot of file-based transaction processing. The processing centers around the conversion of files between numerous formats to suit numerous systems in numerous companies. The processing almost always involves an XML s

  • How to obtain good concurrent read performance from disk 2008-08-12

    I'd like to ask a question then follow it up with my own answer, but also see what answers other people have. We have two large files which we'd like to read from two separate threads concurrently. One thread will sequentially read fileA while the ot

  • How do you create a static class in C++? 2008-08-12

    How do you create a static class in C++? I should be able to do something like: cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl; Assuming I created the BitParser class. What would the BitParser class definition look li

  • In C++/Windows how do I get the network name of the computer I'm on? 2008-08-14

    In a C++ Windows (XP and NT, if it makes a difference) application I'm working on, I need to get the network name associated with the computer the code is executing on, so that I can convert local filenames from C:\filename.ext to \\network_name\C$\f

  • String To Lower/Upper in C++ 2008-08-14

    What is the best way people have found to do String to Lower case / Upper case in C++? The issue is complicated by the fact that C++ isn't an English only programming language. Is there a good multilingual method? --------------Solutions-------------

  • How to overload std::swap() 2008-08-14

    std::swap() is used by many std containers (such as std::list and std::vector) during sorting and even assignment. But the std implementation of swap() is very generalized and rather inefficient for custom types. Thus efficiency can be gained by over

  • Case insensitive string comparison in C++ 2008-08-14

    What is the best way of doing case insensitive string comparison in C++ with out transforming a string to all upper or lower case? Also, what ever methods you present, are they Unicode friendly? Are they portable? --------------Solutions-------------

  • What is the easiest way to parse an INI File in C++? 2008-08-15

    I'm trying to parse an INI file using C++. Any tips on what is the best way to achieve this? Should I use the Windows API tools for INI file processing (with which I am totally unfamiliar), an open-source solution or attempt to parse it manually? ---

  • Strange C++ errors with code that has min()/max() calls 2008-08-18

    I'm seeing strange errors when my C++ code has min() or max() calls. I'm using Visual C++ compilers. --------------Solutions------------- Check if your code is including the windows.h header file and either your code or other third-party headers have

  • How do you create a debug only function that takes a variable argument list? Like printf() 2008-08-18

    I'd like to make a debug logging function with the same parameters as printf. But one that can be removed by the pre-processor during optimized builds. For example: Debug_Print("Warning: value %d > 3!\n", value); I've looked at variadic macros but

  • Can placement new for arrays be used in a portable way? 2008-08-18

    Is it possible to actually make use of placement new in portable code when using it for arrays? It appears that the pointer you get back from new[] is not always the same as the address you pass in (5.3.4, note 12 in the standard seems to confirm tha

  • C++ std::tr2 for VS2005 2008-08-19

    Is Boost the only way for VS2005 users experience TR2? Also is there a idiot proof way of downloading only the TR2 related packages? I was looking at the boost installer provided by BoostPro Consulting. If I select the options for all the threading o

  • How can I sort an array of double pointers based on the values they point to? 2008-08-20

    I am trying to build a function in C/C++ to sort an array and replace each value with its "score" or rank. It takes in a double pointer array to an array of ints, and sorts the double pointers based on the dereferenced value of the integers. I have t

  • Most effective way for float and double comparison 2008-08-20

    What would be the most efficient way to compare two double or two float values? Simply doing this is not correct: bool CompareDoubles1 (double A, double B) { return A == B; } But something like: bool CompareDoubles2 (double A, double B) { diff = A -

  • Is there an efficient way to check for an approximate floating point equality in C++? 2008-08-20

    What would be the most efficient way to compare two double or two float values? Simply doing this is not correct: bool CompareDoubles1 (double A, double B) { return A == B; } But something like: bool CompareDoubles2 (double A, double B) { diff = A -

Copyright (C), All Rights Reserved.

processed in 0.133 (s). 11 q(s)