Efficient conversion of string iterators to pointers

Discussion in 'C++' started by Dom Bannon, Nov 24, 2010.

  1. Dom Bannon

    Dom Bannon Guest

    I have a lot of string-handling code that uses both STL strings and
    C-style pointers, for various reasons. It's not really practical to
    convert all the code to use iterators instead of pointers.

    My problem is that I occasionally have to convert pointers to
    iterators to do something useful. To use 'replace', for example, I
    have to do something like this:

    const char *tkbeg, *tkend; // ptrs in 'linebuf'
    ....
    string::iterator it1 = linebuf.begin() + (tkbeg - linebuf.c_str());
    string::iterator it2 = linebuf.begin() + (tkend - linebuf.c_str());
    linebuf.replace(it1, it2, getReplacement());

    Is there a better way to do this? My concern is that the conversion is
    so complex that the compiler is unlikely to recognise it as a no-op,
    which is presumably what it is for most compilers.

    Thanks -

    Dom
     
    Dom Bannon, Nov 24, 2010
    #1
    1. Advertising

  2. On 11/24/2010 8:17 AM, Dom Bannon wrote:
    > I have a lot of string-handling code that uses both STL strings and
    > C-style pointers, for various reasons.


    Have you examined those reason closely? Are they truly valid? You seem
    concerned about the duality of your system which indicates that you have
    doubts about those reasons.

    > It's not really practical to
    > convert all the code to use iterators instead of pointers.


    If you kept indices instead, you could always convert them to either
    pointers or iterators... Just a thought.

    > My problem is that I occasionally have to convert pointers to
    > iterators to do something useful. To use 'replace', for example, I
    > have to do something like this:
    >
    > const char *tkbeg, *tkend; // ptrs in 'linebuf'
    > ...
    > string::iterator it1 = linebuf.begin() + (tkbeg - linebuf.c_str());
    > string::iterator it2 = linebuf.begin() + (tkend - linebuf.c_str());
    > linebuf.replace(it1, it2, getReplacement());
    >
    > Is there a better way to do this? My concern is that the conversion is
    > so complex that the compiler is unlikely to recognise it as a no-op,


    Which conversion is a no-op? Are you saying that linebuf.begin() and
    linebuf.c_str() return essentially the same thing? Well, they don't.

    > which is presumably what it is for most compilers.


    I don't agree. Iterator and pointers to elements aren't the same thing
    and aren't really meant to be the same thing. However, consider this:
    if instead of keeping pointers to the string elements that you get from
    using 'c_str' member, you actually used the 'data' member, then you
    don't have to convert to iterators when passing to 'replace'. Pass
    those pointers, they are iterators, semantically.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 24, 2010
    #2
    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. Gianni Mariani

    iterators, pointers and (T*)-1

    Gianni Mariani, Oct 21, 2003, in forum: C++
    Replies:
    11
    Views:
    583
    Gianni Mariani
    Oct 22, 2003
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  3. barcaroller
    Replies:
    7
    Views:
    477
    barcaroller
    Nov 8, 2008
  4. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    731
Loading...

Share This Page