string constructor difference

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

    #1

    string constructor difference

    Hi

    Could any one tell me the difference between these 2 string
    constructors ?

    1)string( const char* str );

    2)string( const char* str, size_type length );

    A reference
    http://www.cppreference.com/cppstrin...structors.html says
    that second one creates "a duplicate of str (optionally up to length
    characters long)".Why is the word optionally used there. Does this mean
    that if a '\0' appears in the character array the copying will
    end there ?


    Thanks
    Kiran.

  • Victor Bazarov

    #2
    Re: string constructor difference

    wolverine wrote:
    Could any one tell me the difference between these 2 string
    constructors ?
    >
    1)string( const char* str );
    >
    2)string( const char* str, size_type length );
    >
    A reference
    http://www.cppreference.com/cppstrin...structors.html says
    that second one creates "a duplicate of str (optionally up to length
    characters long)".Why is the word optionally used there.
    Because the clowns who wrote this "reference" couldn't spring for one
    extra description to separate the two constructors. BTW, in some of
    those constructors there is also one more argument - the allocator.
    Does this
    mean that if a '\0' appears in the character array the copying will
    end there ?
    I don't know what this means. The constructor of the standard 'string'
    class that has three arguments:

    strint(char const* str, size_type n, Allocator const &a = Allocator())

    will NOT stop if it encounters a null character. It will copy exactly
    'n' characters from the array the first element of which is pointed to
    by 'str'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • wolverine

      #3
      Re: string constructor difference

      Victor Bazarov wrote:
      wolverine wrote:
      Could any one tell me the difference between these 2 string
      constructors ?

      1)string( const char* str );

      2)string( const char* str, size_type length );

      A reference
      http://www.cppreference.com/cppstrin...structors.html says
      that second one creates "a duplicate of str (optionally up to length
      characters long)".Why is the word optionally used there.
      >
      Because the clowns who wrote this "reference" couldn't spring for one
      extra description to separate the two constructors. BTW, in some of
      those constructors there is also one more argument - the allocator.
      >
      Does this
      mean that if a '\0' appears in the character array the copying will
      end there ?
      >
      I don't know what this means. The constructor of the standard 'string'
      class that has three arguments:
      >
      strint(char const* str, size_type n, Allocator const &a = Allocator())
      >
      will NOT stop if it encounters a null character. It will copy exactly
      'n' characters from the array the first element of which is pointed to
      by 'str'.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Thanks Victor.
      Your answer cleared my doubt partially.

      Comment

      Working...