473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

create my own member functions for strings

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?
Jul 22 '05 #1
5 1420
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #2
> 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%.
Jul 22 '05 #3
Jerry Coffin wrote:
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.

Jul 22 '05 #4
"Gunnar G" <de****@comhem.se> wrote in message
news:jf*******************@newsb.telia.net...
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.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #5
Gunnar G wrote:
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: MLH | last post by:
I was running the following code while logged in as a user belonging only to the Users group. Set usrNew = .CreateUser(Me!UserID) 'The user ID is in a control on the form usrNew.PID =...
7
by: Jimakos Bilakis | last post by:
Hi guys! I'm using the C++ Builder 6 Enterprise Edition where I create some tables in Paradox and with the help of a structure i pass my data from the form (Edit boxes) to the Paradox table with...
5
by: Egbert Nierop \(MVP for IIS\) | last post by:
Boost BSTR performance by 3000% http://technolog.nl/eprogrammer/archive/2006/07/25/1009.aspx
4
by: rhaazy | last post by:
I have created this very primitive class for the purpose of performing some simple recursive functions. I am new to C++ and the concept of classes all together. I put this together using various...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.