[sclug] c++ / g++ 3.3.2 question on using STL transforms
Tim Sutton
t.sutton at reading.ac.uk
Sat Oct 25 09:05:54 UTC 2003
Hi
I am trying to use an STL transform to convert a string to uppercase.
Previously I have used:
std::transform (myString.begin(),myString.end(), myString.begin(), toupper);
successfully. Now it seems things have changed, from what I can make out to
manage issues with unicode upper to lower and lower to upper conversion.
Unfortunately, from the few examples I could find, I am unable to get the
toupper transform to work properly under g++ 3.3.2.
Can anyone help me out here?
Below is a mockup showing what I am trying to do - it segfaults on the
transform line.
Many thanks
Tim
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cctype>
using namespace std;
//this struct is required for stl transform operations
//you used to not need it but now you do as described at:
//http://linux-rep.fnal.gov/software/gcc/onlinedocs/libstdc++/22_locale/
howto.html#7
//and
//
struct ToUpper
{
ToUpper (std::locale const& l) : loc(l) {;}
char operator() (char c) { return std::toupper(c,loc); }
private:
std::locale const& loc;
};
int main(int argc, char *argv[])
{
std::string myString;
//create an instance of the upper case struct as described at
//http://linux-rep.fnal.gov/software/gcc/onlinedocs/libstdc++/22_locale/
howto.html#7
ToUpper myUpperCaseStruct ( std::locale("C") );
//declare the key value
myString=std::string("CRES African climate data");
//convert it to upper case
std::transform (myString.begin(),myString.end(), myString.begin(),
myUpperCaseStruct);
//show the result
cout << "Result is: " << myString << endl;
return EXIT_SUCCESS;
}
--
Tim Sutton
BDWorld Middleware Programmer
-------------------------------------------------------------------
BiodiversityWorld Project
Centre for Plant Diversity & Systematics
School of Plant Sciences
The University of Reading
Reading, RG66AS, UK
Web : www.bdworld.org
Phone : +44-(0)118-378-6052
Email : t.sutton [at] reading.ac.uk
(email preferred method of correspondence)
-------------------------------------------------------------------
More information about the Sclug
mailing list