This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Where is String class?

> On Apr  7, 1999, Ildar Mulyukov <> wrote:
> > substitute MFC's CString class. As I can see "string" class from
> > std/bastring.h has less functionality then I need.
> I don't know what you get get with MFC's CString, but `string' is
> Standard C++, likely to work with any C++ compiler, so it's probably
> the way to go.

I occasionally must deal with MFC stuff.

The only things MFC's CString provides that string does not are:

- trimming of whitespace
- case conversion
- CString::Format

The first two are trivial to do yourself; the third lets you do
sprintf-style conversion without risk of overflow and is nice to
have.  Is that what you need?  It could be written as a non-member
function, e.g.

void sprintf(string& buffer, const char* format, ...);

Our real problem is that we don't have stringstream in libstdc++ 2.

CString suffers from a common programming error that results in
poor performance.  Consider the following code:

CString n_copies_of(const CString& foo, unsigned n)
	CString tmp;
	for (unsigned i = 0; i < n; i++)
		tmp += foo;
	return tmp;

This function is O(n^2), not O(n).  The reason is that each +=
causes a reallocation and copy of the existing string.  Microsoft
applications are full of this kind of thing (quadratic performance
on tasks that can be done in linear time) -- on the other hand,
we should be thankful, as it's created such a big market for high-end
ix86 hardware. :-)

If you replace CString with string in the above function, the
performance is O(n).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]