back_inserter() on strings

Discussion in 'C++' started by Joe Laughlin, Oct 26, 2004.

  1. Joe Laughlin

    Joe Laughlin Guest

    Joe Laughlin wrote:
    > Mike Wahler wrote:
    >> "Joe Laughlin" <> wrote in
    >> message news:...

    <snip>
    >>> Joe Laughlin wrote:
    >>> std::back_insert_iterator<std::string>::eek:perator=(const
    >>> std::back_insert_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_iterator' with a
    >> 'std::string'. ('std::string', while sharing much
    >> commonality with them, isn't strictly a 'container').
    >>
    >> -Mike

    >
    > Argh.
    >
    > From "Accelerated 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."


    Was the book wrong? Or am I reading it wrong?
     
    Joe Laughlin, Oct 26, 2004
    #1
    1. Advertising

  2. Joe Laughlin wrote:
    > Joe Laughlin wrote:
    >>Mike Wahler wrote:
    >>>"Joe Laughlin" <> wrote in
    >>>message news:...

    >
    > <snip>
    >
    >>>>Joe Laughlin wrote:
    >>>>std::back_insert_iterator<std::string>::eek:perator=(const
    >>>>std::back_insert_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_iterator' with a
    >>>'std::string'. ('std::string', while sharing much
    >>> commonality with them, isn't strictly a 'container').
    >>>
    >>>-Mike

    >>
    >>Argh.
    >>
    >>From "Accelerated 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."

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


    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
     
    Victor Bazarov, Oct 26, 2004
    #2
    1. Advertising

  3. Joe Laughlin

    Tom Widmer Guest

    On Tue, 26 Oct 2004 17:50:02 GMT, "Joe Laughlin"
    <> wrote:

    >Joe Laughlin wrote:
    >> Mike Wahler wrote:
    >>> "Joe Laughlin" <> wrote in
    >>> message news:...

    ><snip>
    >>>> Joe Laughlin wrote:
    >>>> std::back_insert_iterator<std::string>::eek:perator=(const
    >>>> std::back_insert_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_iterator' with a
    >>> 'std::string'. ('std::string', while sharing much
    >>> commonality with them, isn't strictly a 'container').
    >>>
    >>> -Mike

    >>
    >> Argh.
    >>
    >> From "Accelerated 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."

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


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

    Tom
     
    Tom Widmer, Oct 28, 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. Mark A. Gibbs

    Re: back_inserter & basic_string

    Mark A. Gibbs, Feb 28, 2004, in forum: C++
    Replies:
    5
    Views:
    604
    Jonathan Turkanis
    Feb 29, 2004
  2. Replies:
    3
    Views:
    418
  3. Replies:
    1
    Views:
    411
    Victor Bazarov
    Feb 16, 2006
  4. Daniel T.

    value_type of back_inserter?

    Daniel T., Mar 31, 2006, in forum: C++
    Replies:
    6
    Views:
    978
    Pete Becker
    Apr 1, 2006
  5. Jess
    Replies:
    17
    Views:
    742
Loading...

Share This Page