Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

opposite of c_str() ??

519 views
Skip to first unread message

Allan Bruce

unread,
Feb 28, 2003, 8:18:20 AM2/28/03
to
Hi there,
I can use c_str() to convert from a string to a char array. How do I do the
opposite?
Thanks
Allan


Ivan Vecerina

unread,
Feb 28, 2003, 8:30:18 AM2/28/03
to
"Allan Bruce" <all...@REMOVEdsl.pipex.com> wrote in message
news:3e5f619e$0$5362$cc9e...@news.dial.pipex.com...

| I can use c_str() to convert from a string to a char array. How do I do
the
| opposite?

If cstr is a pointer to a null-terminated C string, you can construct
an std::string from it.
For example:
return str::string( cstr );

One caveat: cstr must not be a NULL pointer (or behavior will be undefined).


I hope this helps,

--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Soft Dev Manger, xitact <> http://www.xitact.com
Brainbench MVP for C++ <> http://www.brainbench.com

Dean Kutryk

unread,
Feb 28, 2003, 8:31:30 AM2/28/03
to


This worked for me:

#include<iostream>
#include<string>

int main() {

char phrase[] = "Hello ";

std::string s (phrase);
std::cout << s << std::endl;

return 0;
}

Gianni Mariani

unread,
Feb 28, 2003, 10:29:25 AM2/28/03
to Dean Kutryk
Dean Kutryk wrote:
> Allan Bruce wrote:
>
>> Hi there,
>> I can use c_str() to convert from a string to a char array. How do I
>> do the
>> opposite?
>> Thanks
>> Allan
>>
>
>
> This worked for me:
>
> #include<iostream>
> #include<string>
>
> int main() {
>
> char phrase[] = "Hello ";
>
> std::string s (phrase);

This is better.
std::string s (phrase, sizeof(phrase) -1);

For the purpose of this discussion, you're 100% correct, I'm just
pointing out a source of inefficiency I see all too often.

Homer Meyer

unread,
Feb 28, 2003, 2:14:34 PM2/28/03
to

"Gianni Mariani" <gii_...@mariani.ws> wrote in message
news:3E5F8055...@mariani.ws...

Have you timed it? How much less efficient is it really? Does this kind of
thing really make a critical difference in the speed of your code?


Kevin Goodsell

unread,
Feb 28, 2003, 2:28:47 PM2/28/03
to

Assign it.

std::string str;
str = "this is a string";

-Kevin

Neil Butterworth

unread,
Feb 28, 2003, 2:37:14 PM2/28/03
to

"Kevin Goodsell" <good...@bridgernet.com> wrote in message
news:podv5vofglvvr75un...@4ax.com...

Why assign it? why not use the constructor?

std::string str( "this is a string" );


NeilB

Nikhil Tiwari

unread,
Feb 28, 2003, 5:26:44 PM2/28/03
to
Quite honestly, I'm not sure if it makes a difference in this case,
but I know that there certainly are instances when the assignment
operator and the constructor behave differently...

Nikhil

"Neil Butterworth" <neil_but...@lineone.net> wrote in message news:<3e5fba61$1...@mk-nntp-1.news.uk.worldonline.com>...

Neil Butterworth

unread,
Feb 28, 2003, 5:29:33 PM2/28/03
to

"Nikhil Tiwari" <nikhil...@hotmail.com> wrote in message
news:cf059008.03022...@posting.google.com...

>
> "Neil Butterworth" <neil_but...@lineone.net> wrote in message
news:<3e5fba61$1...@mk-nntp-1.news.uk.worldonline.com>...
> > "Kevin Goodsell" <good...@bridgernet.com> wrote in message
> > news:podv5vofglvvr75un...@4ax.com...
> > > On Fri, 28 Feb 2003 13:18:20 -0000, "Allan Bruce"
> > > <all...@REMOVEdsl.pipex.com> wrote:
> > >
> > > >Hi there,
> > > >I can use c_str() to convert from a string to a char array. How do I
do
> > the
> > > >opposite?
> > > >Thanks
> > > >Allan
> > > >
> > >
> > > Assign it.
> > >
> > > std::string str;
> > > str = "this is a string";
> >
> > Why assign it? why not use the constructor?
> >
> > std::string str( "this is a string" );
> >

Please don't top post - rearranged:

> Quite honestly, I'm not sure if it makes a difference in this case,

If you are not sure, then why share your uncertainty with us? I am sure taht
it makes a difference.

> but I know that there certainly are instances when the assignment
> operator and the constructor behave differently...

They behave differently in _all_ circumstances.

NeilB

Alan Krueger

unread,
Feb 28, 2003, 5:47:46 PM2/28/03
to
"Gianni Mariani" <gii_...@mariani.ws> wrote in message
news:3E5F8055...@mariani.ws...
> Dean Kutryk wrote:
[...]

> > int main() {
> >
> > char phrase[] = "Hello ";
> >
> > std::string s (phrase);
>
> This is better.
> std::string s (phrase, sizeof(phrase) -1);
>
> For the purpose of this discussion, you're 100% correct, I'm just
> pointing out a source of inefficiency I see all too often.

...which will fail as soon as someone makes this kind of change:

int main()
{
// ...
// search argv for index of phrase
// ...
char* phrase = argv[index];


std::string s( phrase, sizeof(phrase) - 1 );

// ...
}

Either comment the heck out of that efficiency decision, or use the one that
works in either case. Even though you understand it, you might not notice
it the next time you work on that code, or some C++ newbie will be assigned
to maintain your code and not notice it.

Gianni Mariani

unread,
Feb 28, 2003, 9:42:04 PM2/28/03
to

8 seconds vs 23 seconds .... you guess which one takes longer !


#include <string>

char foo[16384]; // if this is 16 bytes there is still a 30% delta

std::string maker1()
{
return std::string( foo, sizeof(foo) -1 );
}

std::string maker2()
{
return std::string( foo );
}

int main()
{
for ( int i = 0; i < sizeof( foo ) -1; i ++ )
{
foo[i] = 'A';
}

for ( int j = 0; j < 1200000; j ++ )
{
maker2();
}

return 0;
}

Julián Albo

unread,
Mar 1, 2003, 7:10:32 AM3/1/03
to
Gianni Mariani escribió:

> #include <string>
>
> char foo[16384]; // if this is 16 bytes there is still a 30% delta
>
> std::string maker1()
> {
> return std::string( foo, sizeof(foo) -1 );
> }
>
> std::string maker2()
> {
> return std::string( foo );
> }
>
> int main()
> {
> for ( int i = 0; i < sizeof( foo ) -1; i ++ )
> {
> foo[i] = 'A';
> }

You don't zero terminate foo.

Regards.

Fraser Ross

unread,
Feb 28, 2003, 9:09:19 AM2/28/03
to
> If cstr is a pointer to a null-terminated C string, you can construct
> an std::string from it.
> For example:
> return str::string( cstr );
>
> One caveat: cstr must not be a NULL pointer (or behavior will be
undefined).

Use the range constructor if the array is not null terminated.
std::string(cstr, cstr+size_of_cstr);

Fraser.


Gianni Mariani

unread,
Mar 1, 2003, 10:12:20 PM3/1/03
to Julián Albo

Yes I do ! This behaviour is well defined.

Regards
G

Ron Natalie

unread,
Mar 3, 2003, 10:34:37 AM3/3/03
to

"Fraser Ross" <fraserATmembers.v21.co.uk> wrote in message news:3e5f...@news.greennet.net...

He was talking about cstr == NULL, not the string containing it
having a null.


0 new messages