can string be a substitute of char s[] or char* s ?

L

Lingyun Yang

Hi every one,

I am a newbie in C++ and have some experience in C.
My program always seems like a mixture of C and C++,
especially when dealing with string. sometimes I use
std::string but I have some difficult on controlling it
for example:
// format the string as what I like
sprintf(s, "%s%d",buf,i )

I don't know how to do it in std::string in C++

1. Any suggestion about what kind of material I should read
online or book will both be good.

another problem is when I use string under gcc 2.9.*
sometimes met compile error like
<char> ......(I don't remember clearly, but no such problem in
the newest gcc compiler)

so,

2. what about the different gcc version's support of c++ standard ?
 
J

John Harrison

Lingyun Yang said:
Hi every one,

I am a newbie in C++ and have some experience in C.
My program always seems like a mixture of C and C++,
especially when dealing with string. sometimes I use
std::string but I have some difficult on controlling it
for example:
// format the string as what I like
sprintf(s, "%s%d",buf,i )

I don't know how to do it in std::string in C++

One way is

ostringstream tmp;
tmp << buf << i;
string s = tmp.str();

Another way is to look at the format library from boost (www.boost.org)
which lets you do printf like formatting on strings.
1. Any suggestion about what kind of material I should read
online or book will both be good.

Depends on what you know already. I reckon Scott Meyers books Effective C++
and More Effective C++ might be good for you.
another problem is when I use string under gcc 2.9.*
sometimes met compile error like
<char> ......(I don't remember clearly, but no such problem in
the newest gcc compiler)

so,

2. what about the different gcc version's support of c++ standard ?

2.9 is not so good. 3.x is much better.

john
 
D

Daniel T.

Lingyun Yang said:
I am a newbie in C++ and have some experience in C.
My program always seems like a mixture of C and C++,
especially when dealing with string. sometimes I use
std::string but I have some difficult on controlling it
for example:
// format the string as what I like
sprintf(s, "%s%d",buf,i )

I don't know how to do it in std::string in C++

1. Any suggestion about what kind of material I should read
online or book will both be good.

Accelerated C++ would probably be a good choice for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top