🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3914

Re: strings as stl container

#3914
From: Joe Buck
Date: Thu, 04 Apr 1996 18:15
62 lines
2531 bytes
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 
]
Technical Details
Message-ID: <4jvdnd$c9c@hermes.synopsys.com>
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
References: <199604020907.LAA06972@bredex.bredex.de>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
From: jbuck@Synopsys.COM (Joe Buck)
Newsgroups: comp.std.c++
Subject: Re: strings as stl container
Date: 04 Apr 1996 10:15:18 PST
Organization: Synopsys Inc., Mountain View, CA 94043-4033
Approved: austern@isolde.mti.sgi.com
Message-ID: <4jvdnd$c9c@hermes.synopsys.com>
References: <199604020907.LAA06972@bredex.bredex.de>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 4 Apr 1996 02:55:41 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMWQRt0y4NqrwXLNJAQFVCwIAli45uYsIJxE/R7zrjj+vOhMBEpH7hbp2
 bib37fRT6wJV7ocVQ0EEsjKlfEhnbD/9uizBlUIGUE/a9VVIezCToQ==
 =lh7T
Originator: austern@isolde.mti.sgi.com