Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > From char* iterators to char* strings

Reply
Thread Tools

From char* iterators to char* strings

 
 
pmatos
Guest
Posts: n/a
 
      03-07-2005
Hi all,

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???)

Cheers,

Paulo Matos

 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      03-07-2005
"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?


 
Reply With Quote
 
 
 
 
pmatos
Guest
Posts: n/a
 
      03-07-2005

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

 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      03-07-2005
"pmatos" <> wrote in message
news: oups.com...

> 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?


Have you measured it? The differences, if any, would depend on the
particular implementations of strings and memory allocation that you happen
to be using.

> 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.


Or perhaps define a class that lets you get the performance characteristics
you want in a more disciplined way.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
Splitting strings - by iterators? Jeremy Sanders Python 7 02-25-2005 11:17 PM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57