Re: ptr_fun & tolower confusion
On Jul 4, 2:34 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
Soumen wrote:
I wanted convert a mixed case string to a lower case one. And I tried
following code:
std::transform(mixedCaseString.begin(), mixedCaseString::end(),
mixedCaseString.begin(), std::ptr_fun(tolower));
Slightly modified from the archive:
#include <tr1/memory>
#include <cstdlib>
#include <locale>
template < typename CharT >
class to_lower {
typedef std::ctype< CharT > char_type;
std::tr1::shared_ptr< std::locale > the_loc_ptr;
char_type const * the_type_ptr;
public:
to_lower ( std::locale const & r_loc = std::locale() )
: the_loc_ptr ( new std::locale ( r_loc ) )
, the_type_ptr ( &std::use_facet< char_type >( *the_loc_ptr ) )
{}
CharT operator() ( CharT chr ) const {
return ( the_type_ptr->tolower( chr ) );
}
};
TR1's shared_ptr<> class is not nearly as useful in this case as its
bind() routine. In fact, calling TR1's bind() would eliminate the
custom to_lower functor and its attendant complexity.
After all, lowercasing a C++ string seems like it should be a fairly
straightforward task - one that should require only a few lines of
code::
#include <iostream>
#include <string>
#include <algorithm>
#include <locale>
#include <tr1/functional>
using std::locale;
using std::tolower;
using std::tr1::bind;
using std::tr1::placeholders::_1;
int main()
{
std::string s("GrEg");
transform( s.begin(), s.end(), s.begin(),
bind( tolower<char>, _1, locale()));
std::cout << s << "\n";
}
Program Output:
greg