create my own member functions for strings

G

Gunnar G

Hello.
I use the standard C++ string and I'm very pleased with it, but it lack some
functions that I need, like reverse (reverse the string), uppercase, and a
few others. Now I would like to add these functions to string. How do I do
that?
As I see it, I have at least these options:
1) write functions like void reverse(string& s)

2) write my own string class
class mystring{
public:
// stuff..
void reverse(); // or something like that
private:
string _str;
}

As see it, the only loss with alternative 1 is that can't write things like
somestring.reverse(); but I have to write reverse(somestring);

With alternative 2, I have to write everything myself, or can I inherit the
string class in some way so I don't have to write so much code, just adding
the few extra functions?

The over-optimistic solution would perhaps be to
3) modify the string.cpp/string.h files in my system.
4) write a letter to the C++ standardisation organisation

Any thoughts on this before I decide that alternative 1) is the easiest?
 
J

Jerry Coffin

I use the standard C++ string and I'm very pleased with it, but it
lack some
functions that I need, like reverse

A std::string can act like a container so you can use std::reverse on
it:
std::reverse(x.begin(), x.end());
uppercase,

Likewise, you can use transform to apply std::toupper to an entire
string:

std::transform(x.begin(), x.end(), x.begin(), std::toupper);

[ ... ]
The over-optimistic solution would perhaps be to
3) modify the string.cpp/string.h files in my system.
4) write a letter to the C++ standardisation organisation

Any thoughts on this before I decide that alternative 1) is the
easiest?

This would really be a pessimistic solution (though IMO, "problem"
would be more accurate than "solution"). The standard library already
provides what you seem to want, albeit not in the form you seem to have
expected. You mentioned there being other possibilities, and it's
possible the library doesn't cater to them, but without knowing what
they are, it's hard to guess.

I doubt that much more will ever be added to std::string -- in fact,
fairly convincing arguments have been made that it would really be
better to work at stripping out excess functionality instead. IMO,
rather than adding more features unique to std::string, it would be
better to consider things from the opposite direction: making sure that
everything you can do with a string you can do about equally easily by
applying an algorithm to any standard container of your choice. That's
more or less the case already, though in some cases the string-based
version is cleaner or simpler.
 
G

Gunnar G

A std::string can act like a container so you can use std::reverse on
it:
std::reverse(x.begin(), x.end()); Thanks!

making sure that
everything you can do with a string you can do about equally easily by
applying an algorithm to any standard container of your choice. That's
more or less the case already, though in some cases the string-based
version is cleaner or simpler.
I agree to 100%.
 
R

Rolf Magnus

Jerry said:
Likewise, you can use transform to apply std::toupper to an entire
string:

std::transform(x.begin(), x.end(), x.begin(), std::toupper);

However, that won't work correctly for all languages.
 
L

Larry Brasfield

Gunnar G said:
Hello. Hi.
I use the standard C++ string and I'm very pleased with it, but it lack some
functions that I need, like reverse (reverse the string), uppercase, and a
few others. Now I would like to add these functions to string. How do I do
that?
As I see it, I have at least these options:
1) write functions like void reverse(string& s)

A fine option. Most likely for others to readily understand.
2) write my own string class
class mystring{
public:
// stuff..
void reverse(); // or something like that
private:
string _str;
}

This creates interoperability problems with functions
which expect std::string. Until mystring is a widely
accepted alternative, (or wrapper), this is a problem.
As see it, the only loss with alternative 1 is that can't write things like
somestring.reverse(); but I have to write reverse(somestring);

Yes, a terrible loss. Instead of typing 1 dot and 2 parentheses,
you would have to type 2 parentheses. A negative loss during
code writing, and a gain during code reading by others. Maybe
you should take that option out of the loss column.
With alternative 2, I have to write everything myself, or can I inherit the
string class in some way so I don't have to write so much code, just adding
the few extra functions?

That is an option, although it makes a certain rare
kind of code theoretically fail under even more rare
circumstances. If you are dead set upon typing the
extra dot, this could be the way to go.
The over-optimistic solution would perhaps be to
3) modify the string.cpp/string.h files in my system.

An especially bad idea. Note that the type commonly
known as 'string' is not affected by string.h .
4) write a letter to the C++ standardisation organisation

Don't waste your time or their time on this.
Any thoughts on this before I decide that alternative 1) is the easiest?

Yes, it is not only the easiest but the best solution.
 
J

Jeff Flinn

Gunnar said:
Hello.
I use the standard C++ string and I'm very pleased with it, but it
lack some functions that I need,
....

As I see it, I have at least these options:

....

Option 5) see http://www.boost.org/doc/html/string_algo.html

Which is a complete, portable and tested implementation of your option 1. As
with other boost lib's this may eventually be propsosed for inclusion into
the standard.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top