473,385 Members | 1,901 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,385 software developers and data experts.

a question about overloading operator<<

I have a double that I want to output as: (depending on its value)

1000 Bytes
1.6 Kilobyte
2.5 Megabytes
..5 Terabytes

can I do this with...

ostream & operator<<(ostream & o, double n)
How would you recommend going about outputting this?
Jul 22 '05 #1
7 1632
JustSomeGuy wrote:
I have a double that I want to output as: (depending on its value)

1000 Bytes
1.6 Kilobyte
2.5 Megabytes
.5 Terabytes

can I do this with...

ostream & operator<<(ostream & o, double n)
No you cannot. Operators for standard types have been already defined.

How would you recommend going about outputting this?

Just make a wrapper around double:

class BytesFormat {
public:
BytesFormat( double d_ ) : d( d_ ) {}
friend std::ostream &operator<<( std::ostream &os, const BytesFormat
&bt );
private:
double d;
};

void foo()
{
double bytes;
// ...

std::cout << BytesFormat( bytes );
}
std::ostream &operator<<( std::ostream &os, const BytesFormat &bt )
{
if( bt.d < 1000.0 ) os << d << " Bytes";
// ...
return os;
}

--
Regards,
Slava

Jul 22 '05 #2
Vyacheslav Kononenko wrote:
JustSomeGuy wrote:
I have a double that I want to output as: (depending on its value)

1000 Bytes
1.6 Kilobyte
2.5 Megabytes
.5 Terabytes

can I do this with...

ostream & operator<<(ostream & o, double n)
No you cannot. Operators for standard types have been already

defined.

.... but still you can achieve the above goal! All you need to do is
to create a new "num_put" facet (i.e. a class derived from
'std::num_put<char>'), install this facet into a 'std::locale' object,
and 'imbue()' the stream with the resulting object. There is no need
for falling back to ill-advised techniques.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #3
Dietmar Kuehl wrote:
Vyacheslav Kononenko wrote:
JustSomeGuy wrote:
I have a double that I want to output as: (depending on its value)

1000 Bytes
1.6 Kilobyte
2.5 Megabytes
.5 Terabytes

can I do this with...

ostream & operator<<(ostream & o, double n)

No you cannot. Operators for standard types have been already


defined.

... but still you can achieve the above goal! All you need to do is
to create a new "num_put" facet (i.e. a class derived from
'std::num_put<char>'), install this facet into a 'std::locale' object,
and 'imbue()' the stream with the resulting object. There is no need
for falling back to ill-advised techniques.


Cool. How about to print regular doubles and kilobytes at the same time? --
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

--
Regards,
Slava

Jul 22 '05 #4
On Tue, 09 Nov 2004 09:46:56 -0500, Vyacheslav Kononenko
<vy********@NOkononenkoSPAM.net> wrote:
Dietmar Kuehl wrote:
Vyacheslav Kononenko wrote:
JustSomeGuy wrote:

I have a double that I want to output as: (depending on its value)

1000 Bytes
1.6 Kilobyte
2.5 Megabytes
.5 Terabytes

can I do this with...

ostream & operator<<(ostream & o, double n)


No you cannot. Operators for standard types have been already


defined.

... but still you can achieve the above goal! All you need to do is
to create a new "num_put" facet (i.e. a class derived from
'std::num_put<char>'), install this facet into a 'std::locale' object,
and 'imbue()' the stream with the resulting object. There is no need
for falling back to ill-advised techniques.


Cool. How about to print regular doubles and kilobytes at the same time?


You'd have to also write a manipulator to set a custom flag on the
stream and have the num_put facet obtain the formatting information
from the stream (in the ios_base& parameter) before deciding how to
output the number.

This is all a little involved, but you could write a reusable
framework to make it easier (and boost may have one already, just not
in a released version).

Tom
Jul 22 '05 #5
"Dietmar Kuehl" <di***********@yahoo.com> wrote:
Vyacheslav Kononenko wrote:
JustSomeGuy wrote:
I have a double that I want to output as: (depending on its value)

1000 Bytes
1.6 Kilobyte
2.5 Megabytes
.5 Terabytes

can I do this with...

ostream & operator<<(ostream & o, double n)

No you cannot. Operators for standard types have been already

defined.

... but still you can achieve the above goal! All you need to do is
to create a new "num_put" facet (i.e. a class derived from
'std::num_put<char>'), install this facet into a 'std::locale' object,
and 'imbue()' the stream with the resulting object.


You make it sound so easy :)
Jul 22 '05 #6
On 9 Nov 2004 11:41:24 -0800, ol*****@inspire.net.nz (Old Wolf) wrote:
"Dietmar Kuehl" <di***********@yahoo.com> wrote:
Vyacheslav Kononenko wrote:
> JustSomeGuy wrote:
> > I have a double that I want to output as: (depending on its value)
> >
> > 1000 Bytes
> > 1.6 Kilobyte
> > 2.5 Megabytes
> > .5 Terabytes
> >
> > can I do this with...
> >
> > ostream & operator<<(ostream & o, double n)

> No you cannot. Operators for standard types have been already

defined.

... but still you can achieve the above goal! All you need to do is
to create a new "num_put" facet (i.e. a class derived from
'std::num_put<char>'), install this facet into a 'std::locale' object,
and 'imbue()' the stream with the resulting object.


You make it sound so easy :)


There's definitely room for a boost library to make it easier, so that
you can, for example, just change the formatting of double without
having to worry about the other numeric types or formatting options,
etc.

Tom
Jul 22 '05 #7
Old Wolf wrote:
You make it sound so easy :)


That would be because it actually *is* easy! The only tricky part is
the actual formatting since the above is hardly a specification. Here
is some sample code:

| #include <locale>
| #include <iostream>
| #include <sstream>
| #include <algorithm>
|
| struct my_num_put:
| std::num_put<char>
| {
| iter_type do_put(iter_type to, std::ios_base& fmt,
| char fill, long double d) const
| {
| std::ostringstream out;
| out.precision(1);
| out << std::fixed;
| if (d < 1024.0)
| out << d << " Bytes";
| else if (d < 1024.0 * 1024.0)
| out << (d / 1024.0) << " Kilobytes";
| else if (d < 1024.0 * 1024.0 * 1024.0)
| out << (d / (1024.0 * 1024.0)) << " Megabytes";
| else if (d < 0.5 * 1024.0 * 1024.0 * 1024.0 * 1024.0)
| out << (d / (1024.0 * 1024.0 * 1024.0)) << " Gigabytes";
| else
| out << (d / (1024.0 * 1024.0 * 1024.0 * 1024.0))
| << " Terabytes";
|
| std::string const& s = out.str();
| return std::copy(s.begin(), s.end(), to);
| }
| iter_type do_put(iter_type to, std::ios_base& fmt,
| char fill, double d) const
| {
| return do_put(to, fmt, fill, static_cast<long double>(d));
| }
| };
|
| int main()
| {
| std::locale loc;
| std::locale my_loc(loc, new my_num_put);
| std::cout.imbue(my_loc);
|
| std::cout << 1000.0 << "\n";
| std::cout << 1600.0 << "\n";
| std::cout << 2500000.0 << "\n";
| std::cout << 550000000000.0 << "\n";
| }

This is hardly rocket science...

Note a. that this code does indeed just what I decribed before and b.
most of the work goes into actually figuring out the right format. But
even then, it is no big deal.

BTW, if you want to have something like this which you could turn
on/off
at will, you would have to set formatting flags in the stream object to
tell the stream when you want which formatting. The location to store
e.g. a flag is in an 'int' object accessed using the stream's 'iword()'
object. However, this is no big deal either...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #8

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

Similar topics

3
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
10
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
4
by: bart.kowalski | last post by:
Hello, I'm trying to overload operator << for class CString, which has an operator const char *. I thought the following code would do: template <typename T> inline std::basic_ostream<T>...
8
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
3
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion....
1
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
6
by: Peter v. N. | last post by:
Hi all, Maybe this has been asked a million times before. In that case I'm sorry for being to lazy to search the Internet or look it up in a decent C++ reference: I read in O'Reilly's C++...
3
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I...
8
by: Micko1 | last post by:
hi there, I have an issue when overloading <<. string operator << (room * &currentPlayerLocation); string room::operator << (room * &currentPlayerLocation) {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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.