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

Discussion in 'C++' started by Lingyun Yang, Apr 10, 2004.

  1. Lingyun Yang

    Lingyun Yang Guest

    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 ?
    Lingyun Yang, Apr 10, 2004
    #1
    1. Advertising

  2. "Lingyun Yang" <> wrote in message
    news:CeKdc.3021$rg5.28995@attbi_s52...
    > 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
    John Harrison, Apr 10, 2004
    #2
    1. Advertising

  3. Lingyun Yang

    Daniel T. Guest

    Lingyun Yang <> wrote:

    > 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.
    Daniel T., Apr 10, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Kish
    Replies:
    2
    Views:
    906
    Jeff Kish
    Nov 15, 2004
  2. Replies:
    7
    Views:
    765
    Pasi Savolainen
    Feb 26, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,009
    Ian Collins
    May 9, 2006
  4. Replies:
    14
    Views:
    167
    Paul Lalli
    Aug 4, 2005
  5. substitute char

    , Jan 15, 2006, in forum: Perl Misc
    Replies:
    17
    Views:
    188
    robic0
    Jan 22, 2006
Loading...

Share This Page