Re: how to make a functor for min_element
On Dec 28 2010, 10:52 pm, rkaprat <rkap...@gmail.com> wrote:
{ quoted server banner redacted. -mod }
On Dec 26, 8:01 pm, Jun <junh...@gmail.com> wrote:
Dear everyone,
I tried to find the minimal element in a vector, in case
of equality, randomly choose the one of minimal elements.
How to implement it by using std::min_element ?
Thank you in advance.
Happy new year,
Jun
If the vector contains more than one copy of the minimum element, the
iterator points to the first occurrence of the minimum element
code fragments below
typedef std::vector<int, std::allocator<int> > vec_elm;
const vec_elm::value_type v1[] = { 1, 1, 2, 3, 5, 8 };
vec_elm vec(v1 + 0, v1 + sizeof v1 / sizeof *v1);// create vector
vec_elm::iterator itr = std::min_element (vec.begin(), vec.end());//
search for the first smallest element
std::cout<< *itr; //print the answer, ie vec[0]
Would this not do?
{
int v1[] = { 1, 1, 2, 3, 5, 8 };
int* wh = std::min_element (v1,v1+sizeof v1/sizeof int);
std::cout<< *wh;
}
Dosen`t the function accept raw pointers as bidirectional iterators?
regards,
FM.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]