How do I treat situation if the list is empty? I want the program to print "Not possible to find max on an empty list" but the value must be returned...
template<typename T>
T max(const std::list<T>&l){
std::list <T> l1=l;
auto largest=l1.begin();
for(auto it=begin(l1);it!=end(l1);++it){
++largest;
if((*it)>(*largest)){
largest=it;
}
}
return *largest;
}
I tried adding
template<typename T>
T max(const std::list<T>&l){
if(l.empty()){
std::cout<<"List is empty! "<<std::endl;
}
else{
std::list <T> l1=l;
auto largest=l1.begin();
for(auto it=begin(l1);it!=end(l1);++it){
++largest;
if((*it)>(*largest)){
largest=it;
}
}
return *largest;
}
}
In the main.ccp file, the input of element type double is requested, so if, for example, we enter 'b', program should print "List is empty! " and should only put entered doubles into container