- To: oliva@dcc.unicamp.br (Alexandre Oliva)
- Subject: Re: Where is String class?
- From: jbuck@Synopsys.COM (Joe Buck)
- Date: 7 Apr 1999 09:27:37 -0700
- Cc: ildar@faki-campus.mipt.ru, egcs@egcs.cygnus.com
- Newsgroups: cygnus.egcs
- Organization: Cygnus Solutions news/mail gateway
- References: <orpv5g4ng9.fsf@dcc.unicamp.br>
- Xref: cygnus.com cygnus.egcs:23693
> On Apr 7, 1999, Ildar Mulyukov <ildar@faki-campus.mipt.ru> 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).