473,695 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why no operator << for stl containers.

gg
If I have the following class -

template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};

If operator << is provided for stl containers then I can easily
implement dump method as,

os << obj_;

Do I need to provide my own operator << for stl containers?

Why can't stl provide it, as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.

Thanx
gg

Jul 23 '05 #1
6 1773
gg wrote:
If I have the following class -

template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};

If operator << is provided for stl containers then I can easily
implement dump method as,

os << obj_;

Do I need to provide my own operator << for stl containers?
Yes.
Why can't stl provide it,
Because there was no pressing need, I guess. To know for sure, ask in
comp.std.c++ -- they answer the "why" questions.
as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.


Yes, but what to put between them? Try

std::copy(obj_. begin(), obj_.end(),
std::ostream_it erator<T>(os, " "));

V
Jul 23 '05 #2
gg wrote:

Why can't stl provide it, as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.


copy(cont.begin (), cont.end(),
ostream_iterato r<element-type>(cout, " "));

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
gg
My problem is that the parameter T for my template class can be
anything from int, string, stl containers, user defined objects.

I need a generalized solution for dumping all these types.

copy(cont.begin (), cont.end(),
ostream_iterato r<element-type>*(cout, " "));

won't work for other types that can be passed.

Jul 23 '05 #4
gg wrote:
My problem is that the parameter T for my template class can be
anything from int, string, stl containers, user defined objects.

I need a generalized solution for dumping all these types.

copy(cont.begin (), cont.end(),
ostream_iterato r<element-type>*(cout, " "));

won't work for other types that can be passed.


So, define your own operator<< which will use that 'copy' and replace
'element-type' with 'T'.
Jul 23 '05 #5
This might work for some collections:

template < typename Coll >
std::ostream & operator<< ( std::ostream & os, const Coll & coll )
{
typedef typename Coll::value_typ e value_type;
std::copy( coll.begin(), coll.end(),
std::ostream_it erator<value_ty pe>( os, " " ) );
return os;
}

Of course this restricts you to using space as the deilmiter. It won't
work for map (no operator<< for pair),
and you can't define one for pair (for some reason ostream_iterato r
demands the operator<< overload to be
in the same namespace as the class in which the value_type is defined).

You could write your own ostream_iterato r and istream_iterato r, and you
could even write them to get
ostream_iterato r not to put the delimiter at the end of the last
element (You could also write in a custom
opener/delimiter/closer). I think boost have done something like this
already.

Jul 23 '05 #6
In message <jA************ *******@newsrea d1.mlpsca01.us. to.verio.net>,
Victor Bazarov <v.********@com Acast.net> writes
gg wrote:
If I have the following class -
template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};
If operator << is provided for stl containers then I can easily
implement dump method as,
os << obj_;
Do I need to provide my own operator << for stl containers?
Yes.
Why can't stl provide it,


Because there was no pressing need, I guess. To know for sure, ask in
comp.std.c++ -- they answer the "why" questions.
as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.


Yes, but what to put between them?


And *around* them? It's not essential, but operator<< is more useful for
serialisation if there's a paired operator>> to read back what you've
just written.
Try

std::copy(obj_. begin(), obj_.end(),
std::ostream_it erator<T>(os, " "));


--
Richard Herring
Jul 23 '05 #7

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

Similar topics

4
3836
by: Guenther Brunthaler | last post by:
Hi template specialists, I have a problem with the code listed below. What I wanted to do is defining an operator<< that is able to output a 'matrix<ELEMENT_T, INDEX_T>::subrange' object into a 'std::basic_ostream<charT, traits>'. For some reason, it does not work. The compiler always complains:
3
1912
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
3
2042
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
3
2950
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD FUNCTION!!
14
2329
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> using namespace std;
4
2056
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the following will be printed A 3 2
3
1585
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends; // here x contains "004400C8pluto"
2
2176
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
4
2501
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
0
8631
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
8572
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,...
0
9115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8847
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
8825
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5839
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
4340
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...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2272
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.