Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Emmanuel77

To Upper with STL String ?

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Stupid question here, but is there a builtin way to make a ToUpper function with the STL string ? I haven''t a decent STL doc... Thanks for any help, Emmanuel

Share this post


Link to post
Share on other sites
Advertisement
#include <string>
#include <algorithm>
#include <cctype>

std::transform(str.begin(), str.end(), str.begin(), (int (*)(int))std::toupper);


The function cast is required to disambiguate between the toupper function in the <locale> header and the one in the <cctype> header.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan


[edited by - Fruny on May 28, 2004 8:02:13 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
It takes a committee to make something so conceptually simple so complicated...


Considering that they had more than just the english alphabet to handle, it's more complicated "conceptually" than you think.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan


[edited by - Fruny on May 28, 2004 8:04:54 PM]

Share this post


Link to post
Share on other sites
And yet, the implementors of string classes in virtually every other language under the sun managed to offer the functionality to the users in a conceptually simple way.

Share this post


Link to post
Share on other sites
I have very mixed feelings about this and other aspects of the STL.

On one hand, you''re right, this is completely non-obvious to someone who hasn''t dealt with the STL a lot. One would expect something more like myString.to_upper();

On the other hand, it is incredibly flexible, because you can use the same basic transform mechanism with other functions (tolower), or your own custom functions, and with different data types (not just strings). And that is incredibly cool... the problem is, until you''re familiar with the STL it is also incredibly non-obvious and difficult to just ''look up'', which wouldn''t be the case if it were a more traditional API system.



Share this post


Link to post
Share on other sites
quote:
Original post by gmcbay
And that is incredibly cool... the problem is, until you''re familiar with the STL it is also incredibly non-obvious and difficult to just ''look up'', which wouldn''t be the case if it were a more traditional API system.


Which is why I don''t recommend C++ as a beginner language.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
quote:
Original post by gmcbay
I have very mixed feelings about this and other aspects of the STL.

On one hand, you''re right, this is completely non-obvious to someone who hasn''t dealt with the STL a lot. One would expect something more like myString.to_upper();

On the other hand, it is incredibly flexible, because you can use the same basic transform mechanism with other functions (tolower), or your own custom functions, and with different data types (not just strings). And that is incredibly cool... the problem is, until you''re familiar with the STL it is also incredibly non-obvious and difficult to just ''look up'', which wouldn''t be the case if it were a more traditional API system.


But the one doesn''t preclude the other. The interface of the C++ string class is *very* ugly - it looks like the result of someone''s "orthogonal design" wet dream, one which completely ignores the basic needs of the common developer. Uppercasing and lowercasing is a *very* common operation, doing custom transforms is not. But does the API take that into consideration?

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
#include <string>
#include <algorithm>
#include <cctype>

std::transform(str.begin(), str.end(), str.begin(), (int (*)(int))std::toupper);


The function cast is required to disambiguate between the toupper function in the <locale> header and the one in the <cctype> header.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan


[edited by - Fruny on May 28, 2004 8:02:13 PM]


And it''s still not guaranteed to work because (according to the standard) toupper might be given C linkage, but here you''re casting it away so it''s illegal.

Share this post


Link to post
Share on other sites
Uppercase doesn''t even exist in some languages. It''s best not to put something so specific on a general character string implementation. It belongs in locale and deserves to be conceptually separate. People ask questions and code as if ASCII was the only character encoding and that it should be expected to be the default.

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Share this post


Link to post
Share on other sites

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!