Try to use const when you’re passing in a pointer that will not be altered. You shouldn’t be passing as a pointer unless you plan to have the argument as NULL at some point. In this case we always want the vector to have at least one human. It’s a good idea to use std::vector for a container rather than an array if it’s going to be dynamic. This way you don’t need to keep track of how many humans you have.
string max( const std::vector< Human > & _human_vec )
{
std::vector< Human >::iterator human_vec_it = _human_vec.begin();
std::string longest_last_name;
for( human_vec_it; human_vec_it != _human_vec.end(); ++human_vec_it )
{
if( *human_vec_it.lastname.size() > longest_last_name )
longest_last_name = *human_vec_it.lastname;
}
return longest_last_name;
}
This is my first answer on stackoverflow so I hope I’m following the rules correctly. Please advise me if I made a mistake.
solved finding the longest name in a class [closed]