472,779 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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: jGAIS  gais.twbbs.org 
Author: diadia q gais5.cs.ccu.edu.tw o
Jul 23 '05 #1
2 3566
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: jGAIS  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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.