Re: String Iterators
On Apr 18, 9:32 pm, Adrian <n...@bluedreamer.com> wrote:
I want a char * (not const) from a std::string.
I see string.begin() return an iterator which is implementation
defined.
And *iterator returns a reference to the element
So does &*iterator return a pointer to the element?
Yes.
Now this below compiles but is it legal?
No.
#include <iostream>
#include <string>
#include <locale>
int main(int argc, char *argv[])
{
std::string mixed("We ARE a test String");
const std::ctype<char > &ctype=std::use_facet<std::ctype<char >
(std::locale::classic());
std::cout << mixed << std::endl;
// What is a good way to get the pointer from an iterator
// Are you allowed to do this?
ctype.tolower(&*mixed.begin(), &*mixed.end());
No. There are actually two problems, at present:
-- There is currently no requirement that the data in a string
be contiguous; an implementation along the lines of SGI's
rope class is legal, for example.
In fact, no real implementation does this, and the C++
standards committee has decided (for the moment, at least)
to make contiguity a requirement in the next version of the
standard, just as it is for vector. There will also be a
non-const data() to return a pointer to the buffer, so you
don't have to jump through hoops to get it. In the mean
time: all real implementations are contiguous, so you can
jump through hoops, and be relatively safe.
-- The expression *mixed.end() has undefined behavior, and will
probably core dump (assertion failure) in most modern
implementations of the library. To avoid this:
ctype.tolower( &mixed[ 0 ], &mixed[ 0 ] + mixed.size() ) ;
is the consecrated solution. (Whether you use
&*mixed.begin(), or &mixed[0] doesn't matter, but the latter
is shorter to write.)
std::cout << mixed << std::endl;
// instead of this
ctype.toupper(&mixed[0], &mixed[mixed.length()]);
Why "insteamd of"? Both suffer in practice from the same
problem; mixed[mixed.length()] or *mixed.end() actively
dereference one passed the end; in any quality implementation
today, they will provoke an assertion failure.
The basic technique is widely used with vector, and off hand,
I'd say that the &mixed[0] seems to be the preferred technique
for getting the address of the first element---probably just
because it is less to write.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34