This week I’m teaching a course on Advanced C++ in San Diego. I don’t use C++ on a daily basis anymore, but I still feel pretty confident… after all, I’ve been using the language since late 80’s.
I do sometime get stuck on syntax. It doesn’t bother me, as I know I can figure it out pretty quick.

As usual, I love the use of STL and how densely one can write relatively complex algorithms. However, every now and then, I wonder…

The one thing that got me this week was how to convert a standard string to lowercase. Having spent a lot of time lately writing code in languages where the goals of readability and maintainability prevails over performance and tightness, I have to admit I expected to see a upcase/downcase function on the std::string… OK, I should probably have remembered, but I didn’t…

I had to look this one up… Here is the way to downcase string in C++.

// assuming a string s
using namespace std;
transform(
s.begin(),
s.end(),
s.begin(),
bind1st(
mem_fun( &ctype::tolower ),
&use_facet<> >( loc )
)

);

To my students this week, I apologize for not dropping this in from the top of my head…
Seriously, I do see the argument that one should not add too many functions to the string, particularly if it can be done another way… however, converting strings to upper or lower case, is a very frequent task… Would it be too much to ask to add a ‘toupper’ or ‘tolower’ function to the string???
1

View comments

About Me
About Me
Blog Archive
Subscribe
Subscribe
Links
Subscribe
Subscribe
Loading