back_inserter() on strings

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

    back_inserter() on strings

    Joe Laughlin wrote:[color=blue]
    > Mike Wahler wrote:[color=green]
    >> "Joe Laughlin" <Joseph.V.Laugh lin@boeing.com> wrote in
    >> message news:I60Dvs.FqL @news.boeing.co m...[/color][/color]
    <snip>[color=blue][color=green][color=darkred]
    >>> Joe Laughlin wrote:
    >>> std::back_inser t_iterator<std: :string>::opera tor=(const
    >>> std::back_inser t_iterator<std: :string>&)[/color]
    >>
    >> 'back_insert_it erator' requires that the container type
    >> specified by its template argument defines members
    >> 'push_back()' and 'value_type'. 'std::string' defines the
    >> latter, but not the former.
    >>
    >> IOW you cannot use 'back_insert_it erator' with a
    >> 'std::string'. ('std::string', while sharing much
    >> commonality with them, isn't strictly a 'container').
    >>
    >> -Mike[/color]
    >
    > Argh.
    >
    > From "Accelerate d C++", pg 121:
    >
    > "back_inserter( c)
    > Yields an iterator on the container c that appends
    > elements to c. The container must support push_back,
    > which the list, vector, and the string types all do."[/color]

    Was the book wrong? Or am I reading it wrong?


  • Victor Bazarov

    #2
    Re: back_inserter() on strings

    Joe Laughlin wrote:[color=blue]
    > Joe Laughlin wrote:[color=green]
    >>Mike Wahler wrote:[color=darkred]
    >>>"Joe Laughlin" <Joseph.V.Laugh lin@boeing.com> wrote in
    >>>message news:I60Dvs.FqL @news.boeing.co m...[/color][/color]
    >
    > <snip>
    >[color=green][color=darkred]
    >>>>Joe Laughlin wrote:
    >>>>std::back_i nsert_iterator< std::string>::o perator=(const
    >>>>std::back_i nsert_iterator< std::string>&)
    >>>
    >>>'back_insert _iterator' requires that the container type
    >>>specified by its template argument defines members
    >>>'push_back() ' and 'value_type'. 'std::string' defines the
    >>>latter, but not the former.
    >>>
    >>>IOW you cannot use 'back_insert_it erator' with a
    >>>'std::string '. ('std::string', while sharing much
    >>> commonality with them, isn't strictly a 'container').
    >>>
    >>>-Mike[/color]
    >>
    >>Argh.
    >>
    >>From "Accelerate d C++", pg 121:
    >>
    >>"back_inserte r(c)
    >> Yields an iterator on the container c that appends
    >>elements to c. The container must support push_back,
    >>which the list, vector, and the string types all do."[/color]
    >
    >
    > Was the book wrong? Or am I reading it wrong?[/color]

    I can't attest to your reading of it, but the book is not wrong.

    From the Standard, 21.3/2:
    "The template class basic_string conforms to the requirements of
    a Sequence, as specified in (23.1.1). Additionally, because the
    iterators supported by basic_string are random access iterators
    (24.1.5), basic_string conforms to the the requirements of
    a Reversible Container, as specified in (23.1)."

    IOW, a string _is_ a sequential container, no matter how much Mike may
    want to make it sound that it isn't. Whatever _conforms_ to the
    requirements for a container _is_ a container, as far as C++ is concerned.

    V

    Comment

    • Tom Widmer

      #3
      Re: back_inserter() on strings

      On Tue, 26 Oct 2004 17:50:02 GMT, "Joe Laughlin"
      <Joseph.V.Laugh lin@boeing.com> wrote:
      [color=blue]
      >Joe Laughlin wrote:[color=green]
      >> Mike Wahler wrote:[color=darkred]
      >>> "Joe Laughlin" <Joseph.V.Laugh lin@boeing.com> wrote in
      >>> message news:I60Dvs.FqL @news.boeing.co m...[/color][/color]
      ><snip>[color=green][color=darkred]
      >>>> Joe Laughlin wrote:
      >>>> std::back_inser t_iterator<std: :string>::opera tor=(const
      >>>> std::back_inser t_iterator<std: :string>&)
      >>>
      >>> 'back_insert_it erator' requires that the container type
      >>> specified by its template argument defines members
      >>> 'push_back()' and 'value_type'. 'std::string' defines the
      >>> latter, but not the former.
      >>>
      >>> IOW you cannot use 'back_insert_it erator' with a
      >>> 'std::string'. ('std::string', while sharing much
      >>> commonality with them, isn't strictly a 'container').
      >>>
      >>> -Mike[/color]
      >>
      >> Argh.
      >>
      >> From "Accelerate d C++", pg 121:
      >>
      >> "back_inserter( c)
      >> Yields an iterator on the container c that appends
      >> elements to c. The container must support push_back,
      >> which the list, vector, and the string types all do."[/color]
      >
      >Was the book wrong? Or am I reading it wrong?[/color]

      The book is right - std::basic_stri ng::push_back is standard. However,
      VC6 has a pre-standard library in which std::string::pu sh_back is not
      present, which might be the source of the confusion.

      Tom

      Comment

      Working...