where is std::string defined?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Laughlin

    #1

    where is std::string defined?

    #include <iostream>

    int main()
    {
    std::string hello = "Hello World\n";
    return 0;
    }


    This compiles fine. Is std::string defined in cstring? Or is it included
    in iostream?

    Thanks,
    Joe


  • Artie Gold

    #2
    Re: where is std::string defined?

    Joe Laughlin wrote:[color=blue]
    > #include <iostream>
    >
    > int main()
    > {
    > std::string hello = "Hello World\n";
    > return 0;
    > }
    >
    >
    > This compiles fine. Is std::string defined in cstring? Or is it included
    > in iostream?
    >[/color]
    It is defined in <string>. The header <iostream> evidently includes
    <string> on your implementation.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "If you don't think it matters, you're not paying attention."

    Comment

    • Joe Laughlin

      #3
      Re: where is std::string defined?

      Artie Gold wrote:[color=blue]
      > Joe Laughlin wrote:[color=green]
      >> #include <iostream>
      >>
      >> int main()
      >> {
      >> std::string hello = "Hello World\n";
      >> return 0;
      >> }
      >>
      >>
      >> This compiles fine. Is std::string defined in cstring?
      >> Or is it included in iostream?
      >>[/color]
      > It is defined in <string>. The header <iostream>
      > evidently includes <string> on your implementation.
      >
      > HTH,
      > --ag[/color]

      Is it good practice to include the <string> header then?


      Comment

      • Artie Gold

        #4
        Re: where is std::string defined?

        Joe Laughlin wrote:[color=blue]
        > Artie Gold wrote:
        >[color=green]
        >>Joe Laughlin wrote:
        >>[color=darkred]
        >>>#include <iostream>
        >>>
        >>>int main()
        >>>{
        >>> std::string hello = "Hello World\n";
        >>> return 0;
        >>>}
        >>>
        >>>
        >>>This compiles fine. Is std::string defined in cstring?
        >>>Or is it included in iostream?
        >>>[/color]
        >>
        >>It is defined in <string>. The header <iostream>
        >>evidently includes <string> on your implementation.
        >>
        >>HTH,
        >>--ag[/color]
        >
        >
        > Is it good practice to include the <string> header then?
        >
        >[/color]
        Yes. If you use *anything* from the standard library, *always* include
        the header in which it is defined.

        HTH,
        --ag

        --
        Artie Gold -- Austin, Texas

        "If you don't think it matters, you're not paying attention."

        Comment

        • JKop

          #5
          Re: where is std::string defined?

          Joe Laughlin posted:
          [color=blue]
          > Artie Gold wrote:[color=green]
          >> Joe Laughlin wrote:[color=darkred]
          >>> #include <iostream>
          >>>
          >>> int main()
          >>> {
          >>> std::string hello = "Hello World\n";
          >>> return 0;
          >>> }
          >>>
          >>>
          >>> This compiles fine. Is std::string defined in cstring?
          >>> Or is it included in iostream?
          >>>[/color]
          >> It is defined in <string>. The header <iostream>
          >> evidently includes <string> on your implementation.
          >>
          >> HTH,
          >> --ag[/color]
          >
          > Is it good practice to include the <string> header then?[/color]


          I'd advocate it!

          I even go one step futher:


          #include <cstddef>
          #include <cstring>

          inline std::size_t strlenPlusNull( const char* const str);
          {
          std::size_t temp = std::strlen(str );

          return temp += 1;
          }


          The function "strlen" returns an object of type "std::size_ t". As such, if
          you include the header that has "strlen" in it, then it simply must also
          contain the definition of "std::size_ t". But still... I like to include
          "cstddef", whose actual job it is to define "std::size_ t".

          So for your program just there, I myself would do:

          #include <iostream>
          #include <string>


          -JKop

          Comment

          • Jacek Dziedzic

            #6
            Re: where is std::string defined?

            Joe Laughlin wrote:[color=blue]
            > Is it good practice to include the <string> header then?[/color]

            If you intend to use stuff declared in it -- yes, definitely!

            - J.

            Comment

            Working...