////////////////////////////////////// // MT1Q7.cpp - find max in container #include #include #include #include template typename C::value_type maxInContainer(C& c) { C::iterator iter = c.begin(); C::value_type max = *iter; while(iter != c.end()) { if((*iter) > max) max = *iter; ++iter; } return max; } template void showContainer(C& c) { C::iterator iter; for(iter=c.begin(); iter!=c.end(); ++iter) { std::cout << "\n container value is: "; std::cout << (*iter); } std::cout << "\n"; } void main() { std::vector vint; vint.push_back(10); vint.push_back(-7); vint.push_back(23); vint.push_back(6); std::cout << "\n Int Container values are:"; showContainer(vint); int maxInt = maxInContainer(vint); std::cout << "\n largest integer is: " << maxInt << "\n"; std::list dlist; dlist.push_back(-99.9); dlist.push_back(3.14159); dlist.push_back(-1); dlist.push_back(0.111111); std::cout << "\n double Container values are:"; showContainer(dlist); double maxDouble = maxInContainer(dlist); std::cout << "\n largest double is: " << maxDouble << "\n"; std::set charSet; charSet.insert('a'); charSet.insert('k'); charSet.insert('s'); charSet.insert('r'); std::cout << "\n char Container values are:"; showContainer(charSet); char maxChar = maxInContainer(charSet); std::cout << "\n largest char is: " << maxChar << "\n"; }