By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

what difference between c_str() and begin()

P: n/a
string s = "hello";
const char *p = s.begin();
cout << p << endl; // print hello

s = "";

char *p2= s.begin();

cout << p2 << endl; // print hello why?????
I don't know why p2 is not "" ?

if p2 change iterator as fellow
string::iterator p2 = s.begin();

the result is the same
what difference between begin() and c_str()

or my problem should focus on operator<< ?

--
Origin: jGAIS  gais.twbbs.org 
Author: diadia q gais5.cs.ccu.edu.tw o
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
diadia wrote:
string s = "hello";
const char *p = s.begin();
This is not necessarily well-formed. No requirement is placed on the
'std::string::iterator' that it should be convertible to a pointer to
const char. Your program depends on implementation-defined behaviour
and as such is non-portable.
cout << p << endl; // print hello
On your system it might print hello, on my system it may not compile.
s = "";

char *p2= s.begin();

cout << p2 << endl; // print hello why?????
Because the data apparently didn't change. The string simply sets
its size to 0 to indicate that it has no chars stored, most probably.

Whatever you get as the result of outputting the value you obtain by
calling 'begin()' is inconsequential because it's implementation-defined.
I don't know why p2 is not "" ?
Why should it be? What is definite is that p2 == s.end(), and that
should mean that the string is empty. That's the only thing that is
required WRT iterator values, after you clear the string.
if p2 change iterator as fellow
string::iterator p2 = s.begin();

the result is the same
What do you mean "the same"?
what difference between begin() and c_str()
One of them returns an iterator, the other returns a pointer to const
char that points to some place that contains the characters the same as
in the string.
or my problem should focus on operator<< ?


Your problem should focus on the correct use of iterators.

V
Jul 23 '05 #2

P: n/a

"diadia" <di********@gais.twbbs.org> wrote in message
news:4H********@gais.twbbs.org...
string s = "hello";
const char *p = s.begin();
cout << p << endl; // print hello

s = "";

char *p2= s.begin();

cout << p2 << endl; // print hello why?????
I don't know why p2 is not "" ?
It can't be. p2 isn't pointing to a std::string. p2 is pointing to an array
of characters wich expects a \0 terminator to denote the end of the old_type
cstring. In fact, s.c_str() does exactly that, returns a const pointer to a
char array which includes the terminator.

#include <iostream>
#include <iterator>
#include <string>

int main(int argc, char* argv[])
{
std::string s("string");

const char *p = s.c_str(); // not a mismatch
std::cout << p << std::endl;

s = "";

const char *p2 = s.c_str();
std::cout << p2 << std::endl;

s = "modified string";

std::string::iterator iter;
for (iter = s.begin(); iter != s.end(); ++iter)
{
std::cout << *iter; // iterate through each char
}

return 0;
}

A std::string doesn't need a terminator since it maintains a variable to
track its own length (think: container). Initializing a std::string and
ignoring that fact results in undefined behaviour.

if p2 change iterator as fellow
string::iterator p2 = s.begin();

the result is the same
as expected


what difference between begin() and c_str()
Night and day, the first returns a std::string iterator and the second a
constant pointer to a terminated char array.

or my problem should focus on operator<< ?
Has nothing to do with the stream operator.

--
Origin: jGAIS  gais.twbbs.org  Author: diadia q gais5.cs.ccu.edu.tw

o
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.