PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > ostream_iterator less useful than it could be
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
ostream_iterator less useful than it could be

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 16h21   #1
johnbrown105@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ostream_iterator less useful than it could be


I think that ideally, ostream_iterator's operator=() should write the
delimiter *between*
elements and not after them.

One way of doing this:

Add a default int argument = 0 to ostream_iterator's constructor and
save the value
in a member variable counter. Then, in operator=(),

if (++counter == 1)
os << value;
else
os << delimiter << value;
}

Given that "1,2,3,4,5" is a more natural way to write a list than
"1,2,3,4,5,",
is there a particular reason why they did not do it that way?

I suppose that there should also be a method to reset the counter, or
it could be
defined that you cannot reset it, and you must instead create a new
ostream_iterator
if you want to start a new sequence.
  Réponse avec citation
Vieux 07/12/2007, 12h11   #2
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ostream_iterator less useful than it could be

On Dec 6, 5:21 pm, johnbrown...@hotmail.com wrote:
> I think that ideally, ostream_iterator's operator=() should
> write the delimiter *between* elements and not after them.


That's probably more generally useful. Sometimes you want a
terminator, and sometimes you want a separator. If you want a
terminator, but the iterator implements separator, it's easy to
add a final terminator; but if you want a separator, and the
iterator implements terminator, you're stuck---there's no way to
remove something once it's been inserted into the stream.

But this doesn't solve all of the problems, either. Typically,
you need a lot more formatting information. Say, the number of
elements in a line, text to insert before the first element in a
line, text to insert after the last (typically empty), as well
as the separator.

> One way of doing this:


It would be easy to implement. Handling the case of multiline
output (necessary for anything but the shortest sequence) would
be more difficult, but I think it would also be doable.

> Given that "1,2,3,4,5" is a more natural way to write a list than
> "1,2,3,4,5,",
> is there a particular reason why they did not do it that way?


Maybe they were considering sequences of strings, one to a line?
Or they were thinking of machine generated code---C and C++
explicitly allow an extra comma at the end of initializer lists,
for example, precisely so that the simplest situation will work
for code generation.

Maybe no one had actually used it in real applications at the
time it was standardized, so no one had any real idea as to what
was needed?

> I suppose that there should also be a method to reset the
> counter, or it could be defined that you cannot reset it, and
> you must instead create a new ostream_iterator if you want to
> start a new sequence.


I find that in practice, almost every time I'm outputting a
sequence, I'm using something like:

int const maxPerLine = whatever ;
int inLine = 0 ;
while ( begin != end ) {
if ( inLine == 0 ) {
dest << " " ;
} /* sometimes */ else {
dest << " " ;
}
dest << std::setw( n ) << *begin ;
++ begin ;
if ( begin != end ) {
dest << separator ;
}
++ inLine ;
if ( inLine == maxPerLine ) {
dest << '\n' ;
inLine = 0 ;
}
}

I do this offen enough that I'm seriously thinking about
developping some sort of generic solution for it.

On the other hand, about the only time I do it today is for code
generation. And while it's necessary if you want the code to
look pretty, the compiler doesn't care (the direct output of
ostream_iterator would be fine), and no one else should really
have to read it. In the past, with primitive printers and
CRT's, I'd use this anytime I wanted a table, but today, if it's
for the screen, I'll use some sort of layout manager, formatting
each field separately, and if it's for the printer, I'll output
something like:
\begin{tabular}{rrrr}
1&2&3&4\\
abc&def&ghi&jkl\\
\end{tabular}
for the printer front-end (normally LaTeX, since I want the
printed output to look good). I suspect that in the near
future, the output will be XML, with various front-ends to
convert it to whatever is needed.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11459 seconds with 10 queries