Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.
I know how to create
Works for any number of numbers taken from standard input:
#include
#include
#include
int main()
{
std::istream_iterator it_begin(std::cin), it_end;
auto p = std::minmax_element(it_begin, it_end);
if (p.first != it_end)
std::cout << "min: " << *p.first << " max: " << *p.second;
}
Disclaimer:
Technicaly, this isn't required to work by C++ standard. The minimum iterator category required for minmax_element is ForwardIterator which stream iterators are not. Once an input iterator is dereferenced or incremented, its copies are no longer guaranteed to be dereferenceable or comparable to other iterators. It Works On My MachineTM. :)