gleedadswell
gleedadswell

Reputation: 201

c++ syntax to find min in a list

I see some complicated discussion on this topic but I'm failing to make what should be a very simple piece of code work. I've found two ways to make it work but I'm puzzled by the some issues of syntax. I just want to find the minimum value in a list. This works:

using namespace std;

#include <iostream>
#include <list>
#include <algorithm>

int main(void)
{
  auto init_list = {10, 20, 30};
  list<int> alist(init_list);

  list<int>::iterator minnum = min_element(begin(alist), end(alist));

  cout << "The min in the list is: " << *minnum << endl;
  return(1);
}

But there is also the definition of min() which takes an initializer_list as an argument. I think I am misunderstanding the syntax of it. Since I've already defined an init_list I can call

int minint = min(init_list);

and that works. But I don't understand why. The documentation on min says this call should be of form:

template< class T, class Compare >
T min( std::initializer_list<T> ilist, Compare comp );

So shouldn't I have to specify a < comparison operator to be able to use this (apparently not...why?)? However, I don't know how to pass this operator correctly, and have been unable to come up with a clear explanation. Each of the following generate a wide variety of syntax errors:

int minint = min(init_list, operator<);
int minint = min(init_list, int::operator<);

How am I supposed to pass the operator? Clearly it doesn't matter in this case but if my list was not a list of ints but a list of some custom type with overloaded operators then would I need to pass an operator< ? Or does the syntax simply mean that class T must have a comparison operator defined, but you never have to specify it in the min function call?

Upvotes: 2

Views: 10994

Answers (2)

gleedadswell
gleedadswell

Reputation: 201

Thanks Tomasz Klak. I'm pretty new to operator overloading so I didn't know the std::less syntax. That's very useful to know. Clearly I wasn't reading carefully enough, since I missed that other definition of min(). My other question was about how this works with custom objects. I think I've answered that for myself now. Here's what I've found.

You can make an initialization list of custom objects. For example, I have a very simple class that I call Double_list_struct, which is just a container for an int (the index) and a double (the value), with some typical member functions (constructors, getters, setters, toString() and overloaded operators, including operator<). So I can make an initialization list of it by:

Double_list_struct doub1(1,5.5);
Double_list_struct doub2(1,3.5);
Double_list_struct doub3(1,26.5);

auto init_list_doub = {doub1, doub2, doub3};

Having done this the min() function works on it perfectly well.

Double_list_struct mindoub = min(init_list_doub);

I assume (but haven't checked) that if the Double_list_struct class hadn't included an operator< function I would have had to provide one.

Cheers!

Upvotes: 1

tumdum
tumdum

Reputation: 2031

There is version of min (third declaration) which takes only initialization list.

And if you want to be explicit about comparing with < relation, use std::less:

int minint = min(init_list, std::less<int>());

Upvotes: 6

Related Questions