Normalize strings in a container effectively

Having a container of string (vector of emails) and you need to make all the letters uppercase or lowercase or perform whatever else. 

You want to do it as effectively as possible, no copying, no raw loops. Quite a typical use case for std::transform.

This function does exactly that.

std::vector<std::string>& normalize_emails(std::vector<std::string> &emails)
{
  for (auto &email :emails)
    std::transform(email.begin(), email.end(), email.begin(), [](unsigned char c) { return std::tolower(c); });	

  return emails;
}

Transform algorithm is probably the most concise way, the vector is taken by reference and returned by reference as well. Also, all the changes are done inside of the vector, no new allocation happens.

After that, you can make a new vector.

int main()
{
  std::vector<std::string> emails{ "[email protected]" "[email protected]", "[email protected]" };
  std::vector<std::string> vec_emails = normalize_emails(emails);

  for (auto const &i : vec_emails)
    std::cout << i;
     
  return 0;
}