string literals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Buffardi

    string literals

    To those experts in standard C/C++...

    In past I've been quick to use the string.h library for convenience, but now
    I'm working on a project where I'm trying to maximize portability so I want
    to conform to c++ standards as much as I can. Can someone point me in the
    right direction for using character arrays (I assume is the most convenient
    way) specifically with assigning values and passing/returning from
    functions.

    I appreciate it. It's been a while since I've tried manipulating strings
    without string.h and the only C++ book I have with me is specific to Borland
    compilers and not standard C++. Thanks.

    --
    Kevin Buffardi
    "Rockstars -- is there
    anything they don't know?"
    -Homer Simpson


  • Mike Wahler

    #2
    Re: string literals


    "Kevin Buffardi" <kbuff1xw@mwc.e du> wrote in message
    news:bmvo37$pbu 6d$1@ID-80902.news.uni-berlin.de...[color=blue]
    > To those experts in standard C/C++...
    >
    > In past I've been quick to use the string.h library for convenience, but[/color]
    now[color=blue]
    > I'm working on a project where I'm trying to maximize portability so I[/color]
    want[color=blue]
    > to conform to c++ standards as much as I can.[/color]
    [color=blue]
    > Can someone point me in the
    > right direction for using character arrays (I assume is the most[/color]
    convenient[color=blue]
    > way)[/color]

    IMO the more 'convenient' way to work with strings is not
    with arrays at all, but with the std::string type.
    [color=blue]
    > specifically with assigning values and passing/returning from
    > functions.[/color]

    The following (contrived) example demonstrates
    passing strings (by reference) to a function,
    and returning a string from a function:

    #include <iostream>
    #include <string>

    std::string func(const std::string& arg1, const std::string& arg2)
    {
    return arg1 + ' ' + arg2;
    }

    int main()
    {
    std::string s1("Hello");
    std::string s2("world");
    std::cout << func(s1, s2) << '\n'; /* prints "Hello world" */
    return 0;
    }
    [color=blue]
    > I appreciate it. It's been a while since I've tried manipulating strings
    > without string.h and the only C++ book I have with me is specific to[/color]
    Borland[color=blue]
    > compilers and not standard C++.[/color]







    -Mike


    Comment

    • David White

      #3
      Re: string literals

      Kevin Buffardi <kbuff1xw@mwc.e du> wrote in message
      news:bmvo37$pbu 6d$1@ID-80902.news.uni-berlin.de...[color=blue]
      > To those experts in standard C/C++...[/color]

      Standard C++ is all that matters here. C and C++ are different languages and
      there's no such language as C/C++.
      [color=blue]
      > In past I've been quick to use the string.h library for convenience, but[/color]
      now[color=blue]
      > I'm working on a project where I'm trying to maximize portability so I[/color]
      want[color=blue]
      > to conform to c++ standards as much as I can. Can someone point me in the
      > right direction for using character arrays (I assume is the most[/color]
      convenient[color=blue]
      > way) specifically with assigning values and passing/returning from
      > functions.[/color]

      Surmising from your subject line, do you mean string literals only, or other
      character arrays as well? String literals are pretty limited, since the
      strings' contents have to known at compile time and can't be modified. Other
      character arrays are definitely not the most convenient way of assigning
      values and returning strings from functions.

      I suggest that you use std::string objects (use the <string> header file)
      for assigning string contents and returning strings from functions. For
      passing to functions that don't modify the string contents, you can also use
      std::strings, but const char* is often sufficient.

      DW



      Comment

      • Kevin Buffardi

        #4
        Re: string literals

        > IMO the more 'convenient' way to work with strings is not[color=blue]
        > with arrays at all, but with the std::string type.[/color]

        That's standard? I had been using the string type, but I was under the
        impression that it was not standard because the g++ compiler I was using
        threw a fit when I tride to #include<string > .

        --
        Kevin Buffardi
        "Rockstars -- is there
        anything they don't know?"
        -Homer Simpson


        Comment

        • Kevin Buffardi

          #5
          Re: string literals

          > Standard C++ is all that matters here. C and C++ are different languages
          and[color=blue]
          > there's no such language as C/C++.[/color]

          Yes I know "C/C++" isn't a language, but I didn't know if there was a C
          standard for string manipulation that was brought over to C++. In fact, my
          code should also compile on a C-compiler, so either would work... hence my
          original "C/C++" notation.
          [color=blue]
          > Surmising from your subject line, do you mean string literals only, or[/color]
          other[color=blue]
          > character arrays as well? String literals are pretty limited, since the
          > strings' contents have to known at compile time and can't be modified.[/color]

          You're right, the subject line is deceiving. I need to manipulate the
          strings during run-time. I'm looking for a _portable_ solution that should
          work with any C++ compiler on any platform. Given choices, I'd go for the
          most convenient of them if they all satisfy the portability requirement.

          However, if I was wrong and <string> is standard, then I'll just stay with
          it, as it does what I need it to do and I've been using it for years now so
          I'd consider it convenient.

          --
          Kevin Buffardi
          "Rockstars -- is there
          anything they don't know?"
          -Homer Simpson


          Comment

          • Gregg

            #6
            Re: string literals

            "Kevin Buffardi" <kbuff1xw@mwc.e du> wrote in
            news:bmvt8u$rcv us$1@ID-80902.news.uni-berlin.de:
            [color=blue][color=green]
            >> IMO the more 'convenient' way to work with strings is not
            >> with arrays at all, but with the std::string type.[/color]
            >
            > That's standard? I had been using the string type, but I was under
            > the impression that it was not standard because the g++ compiler I was
            > using threw a fit when I tride to #include<string > .[/color]

            Yes, it's standard. You would know that if you had current reference
            material. Set aside most C++ books written before 1998. Get a copy of

            - The C++ Programming Language, 3rd edition, Stroustrup.
            - The C++ Standard Library: A Tutorial and Reference, Nicolai Josuttis.

            I highly recommend the latter if you want to learn about the standard
            library, of which std::string is a part. In addition, if you want an
            introduction to C++ for someone with programming experience, get a copy
            of

            - Accelerated C++, Andrew Koenig, Barbara Moo.

            If your compiler does not accept

            #include <string>

            then it is either old (in the case of gcc, pre-3.0), not installed
            correctly, or not being invoked correctly. Do

            gcc --version

            to see what version you have.

            Gregg

            Comment

            • David White

              #7
              Re: string literals

              Kevin Buffardi <kbuff1xw@mwc.e du> wrote in message
              news:bmvtq0$r95 es$1@ID-80902.news.uni-berlin.de...[color=blue]
              > However, if I was wrong and <string> is standard, then I'll just stay with
              > it, as it does what I need it to do and I've been using it for years now[/color]
              so[color=blue]
              > I'd consider it convenient.[/color]

              Yep, the std::string type is definitely standard and portable.

              DW



              Comment

              • Peter van Merkerk

                #8
                Re: string literals

                > > Standard C++ is all that matters here. C and C++ are different
                languages[color=blue]
                > and[color=green]
                > > there's no such language as C/C++.[/color]
                >
                > Yes I know "C/C++" isn't a language, but I didn't know if there was a[/color]
                C[color=blue]
                > standard for string manipulation that was brought over to C++. In[/color]
                fact, my[color=blue]
                > code should also compile on a C-compiler, so either would work...[/color]
                hence my[color=blue]
                > original "C/C++" notation.[/color]

                If the code should also compile on a C compiler you will have to do
                without to all the benefits of C++, including the std::string class. The
                vast majority of C code compiles also on C++ compilers (unless C99
                extensions are used), including string manipulation. If the code really
                should compile on a C compiler, it is better to start with a C compiler
                first, and test later if it also compiles on a C++ compiler.
                [color=blue]
                > You're right, the subject line is deceiving. I need to manipulate the
                > strings during run-time. I'm looking for a _portable_ solution that[/color]
                should[color=blue]
                > work with any C++ compiler on any platform. Given choices, I'd go for[/color]
                the[color=blue]
                > most convenient of them if they all satisfy the portability[/color]
                requirement.

                If you are willing to say goodbye to C compatibility, std::string meets
                your requirements perfectly.

                --
                Peter van Merkerk
                peter.van.merke rk(at)dse.nl


                Comment

                • Rolf Magnus

                  #9
                  Re: string literals

                  Gregg wrote:
                  [color=blue]
                  > If your compiler does not accept
                  >
                  > #include <string>
                  >
                  > then it is either old (in the case of gcc, pre-3.0),[/color]

                  Actually, even gcc 2.91.66 (the oldest one I have - from early '99)
                  already has that header.

                  Comment

                  • Ron Natalie

                    #10
                    Re: string literals


                    "Kevin Buffardi" <kbuff1xw@mwc.e du> wrote in message news:bmvt8u$rcv us$1@ID-80902.news.uni-berlin.de...[color=blue][color=green]
                    > > IMO the more 'convenient' way to work with strings is not
                    > > with arrays at all, but with the std::string type.[/color]
                    >
                    > That's standard? I had been using the string type, but I was under the
                    > impression that it was not standard because the g++ compiler I was using
                    > threw a fit when I tride to #include<string > .
                    >[/color]
                    What kind of fit? Did you remember it's in namespace std? I use string with
                    G++ all the time.


                    Comment

                    • Andrew Koenig

                      #11
                      Re: string literals

                      > However, if I was wrong and <string> is standard, then I'll just stay with[color=blue]
                      > it, as it does what I need it to do and I've been using it for years now[/color]
                      so[color=blue]
                      > I'd consider it convenient.[/color]

                      <string> is standard. Note, however, that <string.h> is something
                      completely different--it's part of the C standard library, and declares
                      functions such as strcpy.


                      Comment

                      • Kevin Buffardi

                        #12
                        Re: string literals

                        > Did you remember it's in namespace std?

                        Ding ding ding.... we have a winner. Guessing it's been 3 years since I've
                        used string on g++ and completely forgot that it required that one little
                        line. Silly me.


                        --
                        Kevin Buffardi
                        "Rockstars -- is there
                        anything they don't know?"
                        -Homer Simpson


                        Comment

                        Working...