473,715 Members | 3,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostream_iterato r annoyance

consider the following:

ostream_iterato r<int> i(cout,"");
cout << setw(3);
copy(int_list.b egin(),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_iterator s 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_iterato r(ostream&,stri ng) 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 2550
Wiseguy wrote:
ostream iterators are of no use to me unless they can be used to
enforce more complicated text formatting than the
ostream_iterato r(ostream&,stri ng) 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_iterato r <Format> i (cout,"");
transform (int_list.begin (), int_list.end (), i, make_format);
--
Salu2
Jul 23 '05 #2

"Wiseguy" <no***@uber.usa choice.net> wrote in message
news:42******** **@spool9-west.superfeed. net...
| consider the following:
|
| ostream_iterato r<int> i(cout,"");
| cout << setw(3);
| copy(int_list.b egin(),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_iterator s 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_iterato r(ostream&,stri ng) 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_it erator<int> i( std::cout, " " );

Better yet, you might obtain it dynamically:

std::string Width( 4, ' ' );
std::ostream_it erator<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_b ack( 10 );
int_list.push_b ack( 20 );
int_list.push_b ack( 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******@bigpo nd.com.au> scribbled on the stall wall:

"Wiseguy" <no***@uber.usa choice.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_it erator<int> i( std::cout, " " );

Better yet, you might obtain it dynamically:

std::string Width( 4, ' ' );
std::ostream_it erator<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_iterato r 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.usa choice.net> wrote in message
news:42******** **@spool9-west.superfeed. net...
| "Chris \( Val \)" <ch******@bigpo nd.com.au> scribbled on the stall wall:
| >
| > "Wiseguy" <no***@uber.usa choice.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_it erator<int> i( std::cout, " " );
|
| > Better yet, you might obtain it dynamically:
| >
| > std::string Width( 4, ' ' );
| > std::ostream_it erator<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
2038
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
2494
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. Fraser.
8
4615
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 non-zero number follows. ---------------- error message --------------------
4
1993
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
1967
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
939
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 from http://www.camtp.uni-mb.si/books/Thinking-in-C++/Chapter04.html : //: C04:Intset.cpp // Simple use of STL set #include <set>
5
3656
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 define map type with my new type (structure) everything is OK. All compileres I've cheked report an error so I think it is a problem with my code. #include <algorithm> #include <cstddef>
2
7280
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
1380
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 compiler error, and I'm stymied as to why. It's seems to be related to the string type, because iterating over pairs of other types has worked fine for me. The (rather verbose) error is: /usr/include/c++/4.2/bits/stream_iterator.h:196: error: no...
0
8821
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2539
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.