Re: Efficient conversion of string iterators to pointers
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