1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <tuple>
#include <numeric>
std::pair<int,int> min_max( const std::vector<int>& seq )
{
if( seq.empty() ) throw std::domain_error( "min_max of empty sequence" ) ;
const auto iters = std::minmax_element( std::begin(seq), std::end(seq) ) ;
return { *iters.first, *iters.second } ;
}
std::tuple<int,int,double> min_max_avg( const std::vector<int>& seq )
{
const auto mm_pair = min_max(seq) ;
const auto sum = std::accumulate( std::begin(seq), std::end(seq), 0LL ) ;
return std::make_tuple( mm_pair.first, mm_pair.second, double(sum) / seq.size() ) ;
}
int main()
{
const auto tup = min_max_avg( { 7, 2, 9, 4, 0, 5, 1, 6, 8, 3 } ) ;
std::cout << "min: " << std::get<0>(tup) << '\n'
<< "max: " << std::get<1>(tup) << '\n'
<< "avg: " << std::get<double>(tup) << '\n' ;
}
|