From char* iterators to char* strings

Discussion in 'C++' started by pmatos, Mar 7, 2005.

  1. pmatos

    pmatos Guest

    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
    pmatos, Mar 7, 2005
    #1
    1. Advertising

  2. "pmatos" <-id.pt> wrote in message
    news:...

    > 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?
    Andrew Koenig, Mar 7, 2005
    #2
    1. Advertising

  3. pmatos

    pmatos Guest

    Andrew Koenig wrote:
    > "pmatos" <-id.pt> wrote in message
    > news:...
    >
    > > 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
    pmatos, Mar 7, 2005
    #3
  4. "pmatos" <-id.pt> wrote in message
    news:...

    > 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.
    Andrew Koenig, Mar 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    478
    Kai-Uwe Bux
    May 8, 2005
  2. Jeremy Sanders

    Splitting strings - by iterators?

    Jeremy Sanders, Feb 25, 2005, in forum: Python
    Replies:
    7
    Views:
    443
    John Machin
    Feb 25, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,012
    Ian Collins
    May 9, 2006
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
  5. , India
    Replies:
    10
    Views:
    1,061
    James Kanze
    Aug 8, 2009
Loading...

Share This Page