Andrew Koenig wrote:
> "pmatos" <> wrote in message
> news: oups.com...
>
> > I have 2 char* iterators str and end and I'm doing as follows:
> > string id_str(str, end);
> > const char * id = id_str.c_str();
> >
> > but these has 2 problems afaik. One, I'm generating a string as an
> > intermediate step to get a char*, which seems useless. Two, I don't
> > know when id gets destroyed or when the chars to where id points to
are
> > cleaned. I could now use strcopy to copy id to a freshly allocated
> > string but... is there any more direct way? (more efficient
perhaps???)
>
> Well, you can always do this:
>
> size_t len = end - str;
> char* id = new char[len+1];
> std::copy(str, end, id);
> id[len] = '\0';
>
> However, before following this path, I suggest you think about why
you want
> a char* in the first place. It just adds to your bookkeeping
hassles. Why
> not just use the string itself?
Oh well, I might well start a flame war... hope not. However, I'm
programming for efficiency and it seems to me handling char * to be
faster than handling strings. Would I be incorrect for any reason?
Anyway, even if using strings, I'd need to use string * a lot since I'd
be passing them around and I don't wish to be passing them by value.
Cheers,
Paulo Matos
|