Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String Iterators

Reply
Thread Tools

String Iterators

 
 
Adrian
Guest
Posts: n/a
 
      04-18-2007
Hi all,

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?

Now this below compiles but is it legal?

Adrian

#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());
std::cout << mixed << std::endl;

// instead of this
ctype.toupper(&mixed[0], &mixed[mixed.length()]);
std::cout << mixed << std::endl;

return 0;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-18-2007
Adrian 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?
>
> Adrian
>
> #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());


This assumes that the string keeps its characters in an array. I
don't think this is guaranteed anywhere in the Standard. Why can't
you simply use 'transform'?

std::transform(mixed.begin(), mixed.end(), mixed.begin(), tolower);

? Just curious. You can of course write your own 'tolower' that
would use your specific facet, can't you?

> std::cout << mixed << std::endl;
>
> // instead of this
> ctype.toupper(&mixed[0], &mixed[mixed.length()]);
> std::cout << mixed << std::endl;
>
> return 0;
> }


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Adrian
Guest
Posts: n/a
 
      04-18-2007
On Apr 18, 2:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> Adrian wrote:
> This assumes that the string keeps its characters in an array. I
> don't think this is guaranteed anywhere in the Standard.

It doesnt but 21.3.2 does say they conform to sequence containers
which unfortunantley arent all arrays either.

Then also doesnt this apply to using &mixed[0] and
&mixed[mixed.length()]

> Why can't you simply use 'transform'?

Because I was trying to find out how useful facets are. Would be nice
for a simple tolower/toupper functions in the stl that just worked on
strings

Point is moot now though - its an old compiler and doesn support
locale's anyway

> std::transform(mixed.begin(), mixed.end(), mixed.begin(), tolower);
>
> ? Just curious. You can of course write your own 'tolower' that
> would use your specific facet, can't you?

I was looking for something already in the STL so that it would be
more efficent than anything I could write.


Adrian

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-18-2007
Adrian wrote:
> On Apr 18, 2:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> Adrian wrote:
>> This assumes that the string keeps its characters in an array. I
>> don't think this is guaranteed anywhere in the Standard.

> It doesnt but 21.3.2 does say they conform to sequence containers
> which unfortunantley arent all arrays either.
>
> Then also doesnt this apply to using &mixed[0] and
> &mixed[mixed.length()]


Doesn't WHAT apply? The overloaded operator[] returns a reference.
You can take the address of it to get the address of the referred
object.

>
>> Why can't you simply use 'transform'?

> Because I was trying to find out how useful facets are. Would be nice
> for a simple tolower/toupper functions in the stl that just worked on
> strings
>
> Point is moot now though - its an old compiler and doesn support
> locale's anyway
>
>> std::transform(mixed.begin(), mixed.end(), mixed.begin(),
>> tolower);
>>
>> ? Just curious. You can of course write your own 'tolower' that
>> would use your specific facet, can't you?

> I was looking for something already in the STL so that it would be
> more efficent than anything I could write.


You're essentially spending your precious time trying to find some
elusive solution (which may or may not exist) for the sake of some
performance problem you may or may not even have. Does that pretty
much sum up what you're trying to do here?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-19-2007
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:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Adrian
Guest
Posts: n/a
 
      04-19-2007
On Apr 18, 3:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> > Then also doesnt this apply to using &mixed[0] and
> > &mixed[mixed.length()]

>
> Doesn't WHAT apply? The overloaded operator[] returns a reference.
> You can take the address of it to get the address of the referred
> object.

That the elements would have to be an array.

> You're essentially spending your precious time trying to find some
> elusive solution (which may or may not exist) for the sake of some
> performance problem you may or may not even have. Does that pretty
> much sum up what you're trying to do here?

I am sorry I thought this was comp.lang.c++ a discussion group.

If you dont wish to discuss or reply feel free to go to the next
thread

Have a nice day!

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-19-2007
Adrian wrote:
> On Apr 18, 3:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>>> Then also doesnt this apply to using &mixed[0] and
>>> &mixed[mixed.length()]

>>
>> Doesn't WHAT apply? The overloaded operator[] returns a reference.
>> You can take the address of it to get the address of the referred
>> object.

> That the elements would have to be an array.


No. Overloaded operator[] is called for the object and that does
not *at all* imply that there is an array of any kind. Example:
'std::map' has operator[] defined for it, yet, in all implemenations
I've encountered, there was no array. There is a tree (R-B tree, to
be exact), but no array.

Overloading operator[] allows you to use the particular syntax with
an object of that class, the same syntax that you use to index within
an array. Do not confuse the two.

>> You're essentially spending your precious time trying to find some
>> elusive solution (which may or may not exist) for the sake of some
>> performance problem you may or may not even have. Does that pretty
>> much sum up what you're trying to do here?

> I am sorry I thought this was comp.lang.c++ a discussion group.


You thought correctly. And I am discussing. Do you have a problem
with what I said? Would you like to present a rebuttal? By all
means, I'll be happy to discuss.

> If you dont wish to discuss or reply feel free to go to the next
> thread


Oh, thanks for your permission. I didn't know I needed one from you.

> Have a nice day!


You bet!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Adrian
Guest
Posts: n/a
 
      04-19-2007
On Apr 19, 7:58 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> No. Overloaded operator[] is called for the object and that does
> not *at all* imply that there is an array of any kind. Example:
> 'std::map' has operator[] defined for it, yet, in all implemenations
> I've encountered, there was no array. There is a tree (R-B tree, to
> be exact), but no array.
>
> Overloading operator[] allows you to use the particular syntax with
> an object of that class, the same syntax that you use to index within
> an array. Do not confuse the two.

I did not.

You pointed out that since a string does not have to be implented in
an array then
&*mixed.begin() < &*mixed.end() does not have to be true.

I then assumend that &mixed[0] and &mixed[mixed.length()] would follow
the same logic and
not &mixed[0] < &mixed[mixed.length()] does not have to be true.

> >> You're essentially spending your precious time trying to find some
> >> elusive solution (which may or may not exist) for the sake of some
> >> performance problem you may or may not even have. Does that pretty
> >> much sum up what you're trying to do here?

> > I am sorry I thought this was comp.lang.c++ a discussion group.


> You thought correctly. And I am discussing. Do you have a problem
> with what I said? Would you like to present a rebuttal? By all
> means, I'll be happy to discuss.

Certainly Your comment was acerbic. I know you have posted here
for years and I respect your knowledge of c++, sometimes I even laugh
at your short treatment of peoples posts but I just felt my question
didnt justify it such a response. But hey its the usernet - all
opinions are valid.

> > If you dont wish to discuss or reply feel free to go to the next
> > thread

> Oh, thanks for your permission. I didn't know I needed one from you.

lol


Adrian

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-19-2007
Adrian wrote:
> On Apr 19, 7:58 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> No. Overloaded operator[] is called for the object and that does
>> not *at all* imply that there is an array of any kind. Example:
>> 'std::map' has operator[] defined for it, yet, in all implemenations
>> I've encountered, there was no array. There is a tree (R-B tree, to
>> be exact), but no array.
>>
>> Overloading operator[] allows you to use the particular syntax with
>> an object of that class, the same syntax that you use to index within
>> an array. Do not confuse the two.

> I did not.
>
> You pointed out that since a string does not have to be implented in
> an array then
> &*mixed.begin() < &*mixed.end() does not have to be true.
>
> I then assumend that &mixed[0] and &mixed[mixed.length()] would follow
> the same logic and
> not &mixed[0] < &mixed[mixed.length()] does not have to be true.


Ah, got it. Yes, it does not have to be true. I think I understand.

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-19-2007
Adrian wrote:
> Hi all,
>
> I want a char * (not const) from a std::string.
>
>


You could try (modulo length checks on the API call, of course:


#include <string>
#include <vector>

void API_call(char *);

void API_Wrapper(const std::string& s)
{
std::vector<char> v(s.begin(), s.end());
API_call(&v[0]);
}
 
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
Pointers as iterators; vector<string> representation... barcaroller C++ 7 11-08-2008 03:33 PM
Specialising for iterators over contiguous memory (e.g. vector, string) Phil Endecott C++ 9 02-21-2008 06:05 AM
const and non-const string iterators Old Wolf C++ 4 10-16-2006 08:33 AM
Iterators and reverse iterators Marcin Kaliciñski C++ 1 05-08-2005 09:58 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