473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

what difference between c_str() and begin()

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: ¤¤¥¿¤j¾Ç£»GAIS¤ý´Â  gais.twbbs.org 
Author: diadia ±q gais5.cs.ccu.edu.tw µoªí
Jul 23 '05 #1
2 3637
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

"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: ¤¤¥¿¤j¾Ç£»GAIS¤ý´Â  gais.twbbs.org  Author: diadia ±q gais5.cs.ccu.edu.tw

µoªí
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: klaus hoffmann | last post by:
In the following program I would expect r to be a zero terminated string of length 1. Am I wrong? The STL I'm using omit the zeroterm in this situation. Klaus #include <iostream> using...
2
by: Tony Murphy | last post by:
I've got an application that sends emails (not spam!). The application reads a template file (html/text) into a string, the string is processed and placeholders filled in as appropiate. I call a...
15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
2
by: Vyacheslav Kononenko | last post by:
All, If I am not mistaken I had some problems with code like this: std::string foo, bar; .... somefunc( foo.c_str(), bar.c_str() ); Problem was that c_str() used buffer shared btw...
18
by: Metro12 | last post by:
In the <basic_string.h>, I find the implementation of these two functions. But I can't understand the difference between them. Please give me some help! //basic_string::c_str() const _CharT*...
2
by: ma740988 | last post by:
Consider: bool transmit ( const char* pch, size_t len ) { int const val = strcmp( pch, "who_am_i" ); if ( val == 0 ) return ( true ); return ( false ); }
12
by: shyam | last post by:
Hi I have a program within which i have the following statement. sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str( ),SUFFIX.c_str( ) ); Here PREFIX, MIDDLE and SUFFIX are all const...
6
by: bleyddyn.apRhys | last post by:
I have a bit of code that doesn't compile under MSVC++ but does under GCC. Even under GCC, though, it seems odd that I don't need a std namespace qualifier for the exp function. Is this just one...
123
by: plenty900 | last post by:
I was looking over someone's C++ code today and despite having written perfectly readable C++ code myself, the stuff I was looking at was worse than legalese. The people who are guiding the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.