4
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by yangacer
0
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

int lower_case ( int c )
{
  return tolower ( c );
}

int main()
{
  string s = "THIS IS A TEST";

  transform ( s.begin(), s.end(), s.begin(), lower_case );
  cout<< s <<endl;
}

Before you ask, no, you can't safely pass tolower directly to transform in all situations. It's a subtle issue that not many people are aware of, but tolower is an overloaded function and transform is a template function. transform can't deduce which tolower you want, so it's a conflict. You could cast the third argument, but that's a dangerous solution.

-3
#include <functional>
#include <cctype>
#include <string>
#include <algorithm>

//...
string s="ACBCD";
std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::tolower));
Votes + Comments
Don't bump old threads
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.