Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Output iterator

Reply
Thread Tools

Output iterator

 
 
Anders
Guest
Posts: n/a
 
      06-29-2003
Hello there.

Can anyone tell me why the ++-operator is defined for ostream_iterator ? I
can't see the difference between:

ostream_iterator<int> test(cout);
test++ = 1;
test++ = 2;
test++ = 3;
test++ = 4;

and:

ostream_iterator<int> test(cout);
test = 1;
test = 2;
test = 3;
test = 4;

//Anders


 
Reply With Quote
 
 
 
 
Anders
Guest
Posts: n/a
 
      06-29-2003
And yes, I know that I should be using preincrement instead - it's faster:
---
test = 1;
++test = 2;
++test = 3;
++test = 4;
---


//Anders


 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      06-29-2003
In article <3eff61ac$0$32546$(E-Mail Removed)> , Anders
<(E-Mail Removed)> wrote:

| Hello there.
|
| Can anyone tell me why the ++-operator is defined for ostream_iterator ? I
| can't see the difference between:
|
| ostream_iterator<int> test(cout);
| test++ = 1;
| test++ = 2;
| test++ = 3;
| test++ = 4;
|
| and:
|
| ostream_iterator<int> test(cout);
| test = 1;
| test = 2;
| test = 3;
| test = 4;

It is so that generic code will work with ostream_iterator. For
example:

template <class InputIterator, class OutputIterator>
OutputIterator
copy(InputIterator first, InputIterator last, OutputIterator result)
{
for (; first != last; ++first, ++result)
*result = *first;
return result;
}

If you put an ostream_iterator into copy as the OutputIterator, you
really do want it to compile and "do the right thing", which in this
case is nothing for operator++().

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-29-2003
Howard Hinnant wrote:

> In article <3eff61ac$0$32546$(E-Mail Removed)> , Anders
> <(E-Mail Removed)> wrote:
>
> | Hello there.
> |
> | Can anyone tell me why the ++-operator is defined for
> | ostream_iterator ? I can't see the difference between:
> |
> | ostream_iterator<int> test(cout);
> | test++ = 1;
> | test++ = 2;
> | test++ = 3;
> | test++ = 4;
> |
> | and:
> |
> | ostream_iterator<int> test(cout);
> | test = 1;
> | test = 2;
> | test = 3;
> | test = 4;
>
> It is so that generic code will work with ostream_iterator. For
> example:
>
> template <class InputIterator, class OutputIterator>
> OutputIterator
> copy(InputIterator first, InputIterator last, OutputIterator result)
> {
> for (; first != last; ++first, ++result)
> *result = *first;
> return result;
> }
>
> If you put an ostream_iterator into copy as the OutputIterator, you
> really do want it to compile and "do the right thing", which in this
> case is nothing for operator++().


Actually, you must use operator++ to increment the iterator for each
read/write operation, since the implementation might choose to actually
rely on this.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-29-2003
Howard Hinnant wrote:

> In article <bdnq3o$unr$01$(E-Mail Removed)-online.com>, Rolf Magnus
> <(E-Mail Removed)> wrote:
>
> | Howard Hinnant wrote:
> |
> | > If you put an ostream_iterator into copy as the OutputIterator,
> | > you really do want it to compile and "do the right thing", which
> | > in this case is nothing for operator++().
> |
> | Actually, you must use operator++ to increment the iterator for each
> | read/write operation, since the implementation might choose to
> | actually rely on this.
>
> <nod> It might. But the C++ standard says in section 24.5.2.2 -
> ostream_iterator operations, paragraph 3:
>
> | ostream_iterator& operator++();
> | ostream_iterator& operatot++(int);
> |
> | -3- Returns: *this
>
> (i.e. do nothing)


Does that mean the same as if it specifically said that the operator
does nothing?

> I certainly would never write code that used ostream_iterator in an
> unconventional manner that took advantage of this detail. And I'm not
> suggesting anybody should. But if you want to get picky,
> ostream_iterator:perator++() is defined by the C++ standard to do
> nothing but return *this.


I didn't read in the standard, but in TC++PL3, which says:

"The ++ operation might trigger an actual output operation, or it might
have no effect. Different implementations will use different
implementation strategies. Consequently, for code to be portable, a ++
must occur between every two assignments to an ostream_iterator."

> And the main reason that ostream_iterator
> has all of these "extra" operations that do nothing is so that it can
> be used seamlessly as any other iterator that supports the semantics
> required by output_iterator_tag (i.e. output, forward, bidirectional
> and random access iterators).


Ack.

 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      06-30-2003
In article <bdnuim$20g$01$(E-Mail Removed)-online.com>, Rolf Magnus
<(E-Mail Removed)> wrote:

| Howard Hinnant wrote:
|
| > <nod> It might. But the C++ standard says in section 24.5.2.2 -
| > ostream_iterator operations, paragraph 3:
| >
| > | ostream_iterator& operator++();
| > | ostream_iterator& operatot++(int);
| > |
| > | -3- Returns: *this
| >
| > (i.e. do nothing)
|
| Does that mean the same as if it specifically said that the operator
| does nothing?

Hmm... no, I think you're right. It could have other effects, though
I'm having trouble coming up with a quick realistic example. Maybe
some kind of debugging info?

| > I certainly would never write code that used ostream_iterator in an
| > unconventional manner that took advantage of this detail. And I'm not
| > suggesting anybody should. But if you want to get picky,
| > ostream_iterator:perator++() is defined by the C++ standard to do
| > nothing but return *this.
|
| I didn't read in the standard, but in TC++PL3, which says:
|
| "The ++ operation might trigger an actual output operation, or it might
| have no effect. Different implementations will use different
| implementation strategies. Consequently, for code to be portable, a ++
| must occur between every two assignments to an ostream_iterator."

Well, the standard explicitly says that the output will occur under
op=(const T&) operator:

| ostream_iterator& operator=(const T& value );
|
| -1- Effects:
| *out_stream << value;
| if(delim != 0) * out_stream << delim;
| return (*this);

So I don't see how the op++ could also trigger output without really
messing things up.

| Ack.

Bill? Bill The Cat? Is that you?!! We've missed you soooo much!

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments