473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ostream_iterator annoyance

consider the following:

ostream_iterator<int> i(cout,"");
cout << setw(3);
copy(int_list.begin(),int_list.end(),i);

the problem is that the field width restriction is only active for the first
element of int_list. after the first element is copied to cout the stream
setw(3) no longer has any affect.

why? and how to use stream_iterators that don't modify the state of setw()?
ostream iterators are of no use to me unless they can be used to
enforce more complicated text formatting than the
ostream_iterator(ostream&,string) constructor provides for.
--
--
dual 2.8Ghz Xeon; 2GB RAM; 500GB ATA-133; nVidia powered
Linux 2.6.10; glibc-2.3.5; vendor neutral home-brewed installation
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #1
5 2522
Wiseguy wrote:
ostream iterators are of no use to me unless they can be used to
enforce more complicated text formatting than the
ostream_iterator(ostream&,string) constructor provides for.


You can, for example, write a function that takes an int and returns an
object of a class that has an operator << that outputs the int with the
formatting required, and use std::transform with that function.

(Untested code)

class Format {
int num;
public:
Format (n) : num (n) { }
int get () const { return num; }
};

ostream & operator << (ostream & os, const Format & f)
{
os << setw (3) << f.get ();
}

Format make_format (int n)
{
return Format (n);
}

....

ostream_iterator <Format> i (cout,"");
transform (int_list.begin (), int_list.end (), i, make_format);
--
Salu2
Jul 23 '05 #2

"Wiseguy" <no***@uber.usachoice.net> wrote in message
news:42**********@spool9-west.superfeed.net...
| consider the following:
|
| ostream_iterator<int> i(cout,"");
| cout << setw(3);
| copy(int_list.begin(),int_list.end(),i);
|
| the problem is that the field width restriction is only active for the first
| element of int_list. after the first element is copied to cout the stream
| setw(3) no longer has any affect.
|
| why? and how to use stream_iterators that don't modify the state of setw()?
|
|
| ostream iterators are of no use to me unless they can be used to
| enforce more complicated text formatting than the
| ostream_iterator(ostream&,string) constructor provides for.

If you only want a fixed width for each element, then you can
easily set the width of the string:

std::ostream_iterator<int> i( std::cout, " " );

Better yet, you might obtain it dynamically:

std::string Width( 4, ' ' );
std::ostream_iterator<int> i( std::cout, Width.c_str() );

Even better yet, what about using a function object ?:

# include <iostream>
# include <iomanip>
# include <ostream>
# include <list>
# include <algorithm>

inline void SpecialFormat( int n )
{
switch( n )
{
case 1 :
case 2 :
case 5 : std::cout << std::setw( 1 ) << n;
break;
case 10 : std::cout << std::setw( 5 ) << n;
break;
case 20 : std::cout << std::setw( 10 ) << n
<< std::setw( 3 )
<< std::setfill( '.' );
break;
case 30 : std::cout << std::setw( 15 ) << n;
break;
default:
std::cout << std::setw( 2 ) << std::left << n;
break;
}
}

int main()
{
std::list<int> int_list;
int_list.push_back( 10 );
int_list.push_back( 20 );
int_list.push_back( 30 );

std::for_each( int_list.begin(), int_list.end(), SpecialFormat );

return 0;
}

There are other possibilities too.

Hope this help's.

Cheers,
Chris Val
Jul 23 '05 #3
"Chris \( Val \)" <ch******@bigpond.com.au> scribbled on the stall wall:

"Wiseguy" <no***@uber.usachoice.net> wrote in message
news:42**********@spool9-west.superfeed.net...
If you only want a fixed width for each element, then you can
easily set the width of the string:

std::ostream_iterator<int> i( std::cout, " " );

Better yet, you might obtain it dynamically:

std::string Width( 4, ' ' );
std::ostream_iterator<int> i( std::cout, Width.c_str() );

how does that enforce a field width?...the above would just ALWAYS put
n-spaces after each item sent to the stream, right? that's not what I
want because my purpose is to use the iterators in stl algorithms like
copy.

Even better yet, what about using a function object ?:

inline void SpecialFormat( int n )
{
}


unfortunately the function object is not generic enough and needs to work with
stl algorithms. that's the whole purpose behind my quest: a generic ways of
using iterators that preserve the special formatting and (state) information of
the stream they are acting upon.

seems that for something like

o << setw(4) << something;

the field width restriction is only valid for the duration of the previous
statement and that adding another

o << somethign_else;

would NOT respect the previous setw()...that's the point of my contention.

so, a more related question would be "how to make something like setw()
permanent for the duration of a stream's existence?"
--
--
dual 2.8Ghz Xeon; 2GB RAM; 500GB ATA-133; nVidia powered
Linux 2.6.10; glibc-2.3.5; vendor neutral home-brewed installation
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #4
Wiseguy wrote:

so, a more related question would be "how to make something like setw()
permanent for the duration of a stream's existence?"


That's not _really_ what you want. That would affect all insertions, not
just the ones you're currently interested in.

The way to do it is to write your own variation of ostream_iterator that
does a setw before each insertion. It's not very complicated, just a bit
tedious.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5

"Wiseguy" <no***@uber.usachoice.net> wrote in message
news:42**********@spool9-west.superfeed.net...
| "Chris \( Val \)" <ch******@bigpond.com.au> scribbled on the stall wall:
| >
| > "Wiseguy" <no***@uber.usachoice.net> wrote in message
| > news:42**********@spool9-west.superfeed.net...
| >>
| > If you only want a fixed width for each element, then you can
| > easily set the width of the string:
| >
| > std::ostream_iterator<int> i( std::cout, " " );
|
| > Better yet, you might obtain it dynamically:
| >
| > std::string Width( 4, ' ' );
| > std::ostream_iterator<int> i( std::cout, Width.c_str() );
| >
|
| how does that enforce a field width?...the above would just ALWAYS put
| n-spaces after each item sent to the stream, right?

Right.

| that's not what I want because my purpose is to use the iterators
| in stl algorithms like copy.
|
| > Even better yet, what about using a function object ?:
| >>
| > inline void SpecialFormat( int n )
| > {
| > }
|
| unfortunately the function object is not generic enough and needs to work with
| stl algorithms. that's the whole purpose behind my quest: a generic ways of
| using iterators that preserve the special formatting and (state) information of
| the stream they are acting upon.
|
| seems that for something like
|
| o << setw(4) << something;
|
| the field width restriction is only valid for the duration of the previous
| statement and that adding another
|
| o << somethign_else;
|
| would NOT respect the previous setw()...that's the point of my contention.

Nor should it.

| so, a more related question would be "how to make something like setw()
| permanent for the duration of a stream's existence?"

I'm sorry, but I agree with 'Pete Becker' here.

Cheers,
Chris Val
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Mantoulidis | last post by:
Hello all... I get an error message for the following program by the compiler: #include <iostream> #include <string> using namespace std; ostream_iterator<string> oo(cout);
12
by: Fraser Ross | last post by:
a std::ostream_iterator can be constructed from a ostream but not a fstream. If the fstream is opened with the out mode how can I get a ostream_iterator? I am not using a pointer to any base class....
8
by: Jeff | last post by:
std::copy to std::ostream_iterator doesn't seem to work with maps and pairs. Shouldn't I expect something like this to work? The G++ error message when USE_COPY_TO_PRINT is defined as a...
4
by: Nan Li | last post by:
Hello, Can any one get the 'copy' statement below to work? I want it to do the same thing as the 'for' loop does. But I got a lot of STL error during compile. Thanks, Nan #include <map>
2
by: alzforu | last post by:
The code in which i m facing the problem is: //file stream.cc #include <vector> #include <algorithm> #include <iostream> using namespace std;
3
by: Evyn | last post by:
Hi, Can someone tell me why I always get an error like "`ostream_iterator' undeclared (first use this function) " when using ostream_iterator. I have for example tried the following code taken...
5
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
0
by: ShaunJ | last post by:
I'm trying to use an ostream_iterator to iterate over a set of pairs. I've used an ostream_iterator over other types and had no problem. However, with the following code snippet, I'm getting a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.