Topic: strings as stl container


Author: Nico Josuttis <nico@bredex.de>
Date: 1996/04/11
Raw View
In article <4jvdnd$c9c@hermes.synopsys.com>,
Joe Buck <jbuck@Synopsys.COM> wrote:
>Nico Josuttis <nico@bredex.de> writes:
>>Due to the fact that strings have iterator support,
>>the could be used as stl container.
>
>Correct.
>
>>But two things are missing:
>> - push_back()
>> - clear()
>
>No, these are not needed.
clear() is required for sequenceable containers,
push_back() is optional for sequenceable containers but would make
many sense.
So for the others vector like operations
(pop_back(), front(), back()).

>
>>These are fundamental operations, that the corresponding "normal"
>>container, namely vector, has.
>
>The documentation I have does not list clear as a member of vector.
>
Oh yes, it does, see 23.2.5.6 in vector class structure.

>>Especially push_back() would be essential to use insert iterators
>>for strings.
>
>No, this isn't true.  You need push_back to use back_insert_iterator,
>but insert_iterator only needs insert, which string does have.
>
But insert_iterators are VERY dangerous for sequenceable collections.
They invalidate themself if the position parameter is not end().
So having push_back() would be safer and of course more convenient
(otherwise there is no sense to have it for vectors either).

--
Nico                             address: BREDEX GmbH, Nicolai Josuttis
email:   nico@bredex.de                   Fallersleber-Tor-Wall 23
phone:   +49 531 24330-0                  D-38100 Braunschweig
fax:     +49 531 24330-99                 Germany
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Nico Josuttis <nico@bredex.de>
Date: 1996/04/02
Raw View
Due to the fact that strings have iterator support,
the could be used as stl container.
But two things are missing:
 - push_back()
 - clear()
These are fundamental operations, that the corresponding "normal"
container, namely vector, has.
Especially push_back() would be essential to use insert iterators
for strings.
Wouldn't it make sense to introduce them for strings ?

--------
Nico                             address: BREDEX GmbH
email:   nico@bredex.de                   Nicolai Josuttis
                                          Fallersleber-Tor-Wall 23
phone:   +49 531 24330-0                  D-38100 Braunschweig
fax:     +49 531 24330-99                 Germany
--------


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jbuck@Synopsys.COM (Joe Buck)
Date: 1996/04/04
Raw View
Nico Josuttis <nico@bredex.de> writes:
>Due to the fact that strings have iterator support,
>the could be used as stl container.

Correct.

>But two things are missing:
> - push_back()
> - clear()

No, these are not needed.

>These are fundamental operations, that the corresponding "normal"
>container, namely vector, has.

The documentation I have does not list clear as a member of vector.

>Especially push_back() would be essential to use insert iterators
>for strings.

No, this isn't true.  You need push_back to use back_insert_iterator,
but insert_iterator only needs insert, which string does have.

Try the following program (works with gcc 2.7.2/libg++ 2.7.1 and appears
to conform to the standard if I didn't screw up):

----------------------
#include <string>
#include <iterator>
#include <algorithm>

const char text[] = "This is text\n";
const char text2[] = "even more ";

int main()
{
    string foo;
    insert_iterator<string> iter = inserter(foo, foo.begin());
    copy(text, text + sizeof(text)-1, iter);
    cout << "value of foo: " << foo;

    copy(text2, text2 + sizeof(text2)-1, inserter(foo, foo.begin()+8));
    cout << "value of foo: " << foo;
}
----------------------

This prints
value of foo: This is text
value of foo: This is even more text

--
-- Joe Buck  <jbuck@synopsys.com> (not speaking for Synopsys, Inc)

Work for something because it is good,
not just because it stands a chance to succeed.    -- Vaclav Havel
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Nathan Myers, http://www.cantrip.org/" <ncm@cantrip.org>
Date: 1996/04/05
Raw View
Joe Buck wrote:

> Nico Josuttis <nico@bredex.de> writes:
> >Due to the fact that strings have iterator support,
> >they could be used as stl container.
>
> >But two things are missing:
> > - push_back()
> > - clear()
>
> No, these are not needed.

The January '96 WP lists clear() as a required member of
a Sequence, and push_back() as an optional member to be
"provided only for the containers for which they take
constant time".  In that version of the WP, some of the
optional members are provided, others are not.

These omissions probably should be listed as an open issue for
the Strings chapter.

Nathan Myers
ncm@cantrip.org
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]