std::string conversion question 
Author Message
 std::string conversion question

    I know that there has to be an easy way to convert a std::string object
to uppercase, but I have not been able to find it.  The only way I've seen
seems pretty clumsy ( _strupr(&string[0])).  Are there some string format
methods I'm missing?

                John



Mon, 16 Apr 2001 03:00:00 GMT  
 std::string conversion question

Quote:

>    I know that there has to be an easy way to convert a std::string object
>to uppercase, but I have not been able to find it.  The only way I've seen
>seems pretty clumsy ( _strupr(&string[0])).  Are there some string format
>methods I'm missing?

    First of all, besides being clumsy, that's not guaranteed to work, as
std::strings are necessarily null terminated, and (I don't believe)
&string[0] is necessarily followed by &string[1].

    Fortunately, the Standard Library gives you all the tools you'll ever
need; you just  need to know how to apply them:

 string hello = "hello world";
 std::transform(hello.begin(), hello.end(), hello.begin(), toupper);
 std::cout << hello << endl;

    if the transform line look a bit unwieldy, it's easily reigned in:

    inline void StringUpper(std::string str)
    {   std::transform(str.begin(), str.end(), str.begin(), toupper);    }

    In fact, it's even simple to create a copying strupr:

    inline std:string StringUpperCopy(const std::string str)
    {
            std::string    sOut(str.size());    // set size of new string
            std::transform(str.begin(), str.end(), sOut.begin(), toupper);
            return sOut;
    }

--
Truth,
   James [MVP]
http://www.NJTheater.Com       -and-
http://www.NJTheater.Com/JamesCurran

begin 666 James M. Curran.vcf


M4DLZ.SLQ."!*;VAN(%-T+"!3=6ET92 R0CM";&]O;69I96QD.TY*.S W,# S
M+34Q-#D-"DQ!0D5,.U=/4DL[14Y#3T1)3D<]455/5$5$+5!224Y404),13HQ
M."!*;VAN(%-T+"!3=6ET92 R0CTP1#TP04)L;V]M9FEE;&0L($Y*(# W,# S
M+34Q-#D-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VTO2F%M97-#=7)R
M86X-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VT-"D5-04E,.U!2148[
M24Y415).150Z2F%M97-#=7)R86Y 0V]M<'5397)V92YC;VT-"E)%5CHQ.3DX

`
end



Mon, 16 Apr 2001 03:00:00 GMT  
 std::string conversion question
You can also use the Standard Library's ctype template which provides a
'tolower' member function.  For example:

    string myString("THIS IS A STRING");
    std::ctype<char>().tolower(myString.begin(), myString.end());

I have no idea if this is any faster or slower than the transform algorithm
that JC presented.  Just another way...

Joe O'

Quote:

>    Fortunately, the Standard Library gives you all the tools you'll ever
>need; you just  need to know how to apply them:

> string hello = "hello world";
> std::transform(hello.begin(), hello.end(), hello.begin(), toupper);
> std::cout << hello << endl;

>    if the transform line look a bit unwieldy, it's easily reigned in:

>    inline void StringUpper(std::string str)
>    {   std::transform(str.begin(), str.end(), str.begin(), toupper);    }

>    In fact, it's even simple to create a copying strupr:

>    inline std:string StringUpperCopy(const std::string str)
>    {
>            std::string    sOut(str.size());    // set size of new string
>            std::transform(str.begin(), str.end(), sOut.begin(), toupper);
>            return sOut;
>    }

>--
>Truth,
>   James [MVP]
>http://www.NJTheater.Com       -and-
>http://www.NJTheater.Com/JamesCurran



Sun, 22 Apr 2001 03:00:00 GMT  
 std::string conversion question
On Thu, 29 Oct 1998 14:33:17 -0500, "James Curran"

Quote:

>    inline void StringUpper(std::string str)
>    {   std::transform(str.begin(), str.end(), str.begin(), toupper);    }

This function fails as written, because the string is passed by value.
The transform itself is performed on the function's local copy of the
string. It should be:

inline void StringUpper(std::string& str)
{   std::transform(str.begin(), str.end(), str.begin(), toupper);    }

Quote:

>    inline std:string StringUpperCopy(const std::string str)
>    {
>            std::string    sOut(str.size());    // set size of new string
>            std::transform(str.begin(), str.end(), sOut.begin(), toupper);
>            return sOut;
>    }

Since this one returns its result, no problem.

I verified my observations with a small test program.

--
Mike Enright

(Not speaking for Cetasoft)



Sun, 22 Apr 2001 03:00:00 GMT  
 std::string conversion question

Quote:

>You can also use the Standard Library's ctype template which provides a
>'tolower' member function.  For example:

>    string myString("THIS IS A STRING");
>    std::ctype<char>().tolower(myString.begin(), myString.end());

>I have no idea if this is any faster or slower than the transform algorithm
>that JC presented.  Just another way...

Unfortunately, there's no guarantee that will work. That tolower is defined
in terms of pointers, not iterators, and std::string::iterator isn't
required to be a pointer.

--
Doug Harrison



Sun, 22 Apr 2001 03:00:00 GMT  
 std::string conversion question
       Ooops.... I testes the transform call, but not the function
surrounding it.   Go figure --- the one time I leave off the "WARNING -
Untested code" header on something I post, is the time I{*filter*}it up....

--
Truth,
   James [MVP]
http://www.*-*-*.com/       -and-
http://www.*-*-*.com/

Quote:

>>    inline void StringUpper(std::string str)
>>    {   std::transform(str.begin(), str.end(), str.begin(), toupper);    }

>This function fails as written, because the string is passed by value.
>The transform itself is performed on the function's local copy of the
>string. It should be:

>inline void StringUpper(std::string& str)
>{   std::transform(str.begin(), str.end(), str.begin(), toupper);    }

begin 666 James M. Curran.vcf


M4DLZ.SLQ."!*;VAN(%-T+"!3=6ET92 R0CM";&]O;69I96QD.TY*.S W,# S
M+34Q-#D-"DQ!0D5,.U=/4DL[14Y#3T1)3D<]455/5$5$+5!224Y404),13HQ
M."!*;VAN(%-T+"!3=6ET92 R0CTP1#TP04)L;V]M9FEE;&0L($Y*(# W,# S
M+34Q-#D-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VTO2F%M97-#=7)R
M86X-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VT-"D5-04E,.U!2148[
M24Y415).150Z2F%M97-#=7)R86Y 0V]M<'5397)V92YC;VT-"E)%5CHQ.3DX

`
end


Mon, 23 Apr 2001 03:00:00 GMT  
 std::string conversion question
You're right, I can't believe I missed that. I've got some code I ought to
change.

Anyway, I did some performance tests and it turns out that that James
Curran's method is 30% faster than mine anyway (in a Unicode, Release
build).

Joe O'

Quote:


>>You can also use the Standard Library's ctype template which provides a
>>'tolower' member function.  For example:

>>    string myString("THIS IS A STRING");
>>    std::ctype<char>().tolower(myString.begin(), myString.end());

>>I have no idea if this is any faster or slower than the transform
algorithm
>>that JC presented.  Just another way...

>Unfortunately, there's no guarantee that will work. That tolower is defined
>in terms of pointers, not iterators, and std::string::iterator isn't
>required to be a pointer.

>--
>Doug Harrison




Mon, 23 Apr 2001 03:00:00 GMT  
 std::string conversion question

Quote:

>You're right, I can't believe I missed that. I've got some code I ought to
>change.

>Anyway, I did some performance tests and it turns out that that James
>Curran's method is 30% faster than mine anyway (in a Unicode, Release
>build).

Unfortunately, there's still a potential problem with:

 string hello = "hello world";
 std::transform(hello.begin(), hello.end(), hello.begin(), toupper);

By default, VC's chars are signed, and if your string contains some chars
that are negative, it isn't going to work, because ::toupper's argument is
an int, and toupper only works on int values that are in the range of
unsigned char plus EOF. So basically you have to write a functions such as:

 char safe_toupper(char x)
 {
    return static_cast<char>(toupper(static_cast<unsigned char>(x)));
 }

and pass that to transform. While std::ctype<char>::toupper is suitably
typed to avoid this issue, it's a non-static member, so you can't just pass
it to transform. There's also the template function std::toupper, but you
have to worry about its pesky, required locale argument. So, there are
several approaches to writing a C++ strupr for std::strings, and none of
them is particularly pretty.

--
Doug Harrison



Mon, 23 Apr 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. System.String to std::string conversion

2. basic std::stringstream and std::string questions

3. convert non std::string to std::string

4. BSTR to std::string conversion ?

5. BSTR to std::string conversion ?

6. std::string to CString conversion

7. std::string to CString conversion

8. std::string support for ASCII conversion

9. String conversion and double-to-string conversion

10. String conversion and double-to-string conversion

11. String conversion and double-to-string conversion

12. BSTR to std::string conversion and std::string to BSTR?

 

 
Powered by phpBB® Forum Software