Topic: Overloading to_string
Author: noreply@firemath.info
Date: Mon, 4 Feb 2013 07:27:40 -0800 (PST)
Raw View
------=_Part_365_12941967.1359991660879
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
I would like to see additional parameters for functions to_string. Behavior
like in the functions below could be one way for making life easier.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
std::string tostring( double val,
int width= 0,
int precision= std::ostringstream().precision(),
char fill= ' ',
std::ios_base::fmtflags fmt= std::ostringstream().flags() )
{
std::ostringstream ost;
ost.setf( fmt );
ost << std::setw(width) << std::setfill(fill)
<< std::setprecision(precision)
<< val;
return ost.str();
}
std::string tostring( int val,
int width= 0,
char fill= ' ',
int base= 10,
std::ios_base::fmtflags fmt= std::ostringstream().flags() )
{
std::ostringstream ost;
ost.setf( fmt );
ost << std::setw(width) << std::setfill(fill)
<< std::setbase(base)
<< val;
return ost.str();
}
int main()
{
using namespace std;
double my_e= -2.7182818284590452354;
int my_i= -314159265;
cout << "(1)" << tostring( my_e ) << "end\n";
cout << "(2)" << tostring( my_e, 12 ) << "end\n";
cout << "(3)" << tostring( my_e, 12, 12, ' ' ) << "end\n";
cout << "(4)" << tostring( my_e, 12, 6, '0' ) << "end\n";
cout << "(5)" << tostring( my_e, 16, 6, ' ',
ios_base::scientific | ios_base::left )
<< "end\n";
cout << "(6)" << tostring( my_e, 12, 6, ' ', ios_base::internal )
<< "end\n";
cout << "\n(7)" << tostring( my_i ) << "end\n";
cout << "(8)" << tostring( my_i, 12, ';' ) << "end\n";
cout << "(9)" << tostring( my_i, 12, '0', 10, ios_base::internal )
<< "end\n";
cout << "(A)" << tostring(-my_i, 12, ' ', 16, ios_base::showbase )
<< "end\n";
/* Yields:
(1)-2.71828end
(2) -2.71828end
(3)-2.71828182846end
(4)0000-2.71828end
(5)-2.718282e+00 end
(6)- 2.71828end
(7)-314159265end
(8);;-314159265end
(9)-00314159265end
(A) 0x12b9b0a1end
*/
}
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.
------=_Part_365_12941967.1359991660879
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hi all,<br><br>I would like to see additional parameters for functions to_s=
tring. Behavior like in the functions below could be one way for making lif=
e easier.<br><br><br>#include <iostream><br>#include <iomanip><=
br>#include <sstream><br>#include <string><br><br>std::string t=
ostring( double val,<br> int width=3D 0,<br> =
int precision=3D std::ostringstream().precision(),<br>&=
nbsp; char fill=3D ' ',<br> std=
::ios_base::fmtflags fmt=3D std::ostringstream().flags() )<br>{<br>&n=
bsp; std::ostringstream ost;<br> ost.setf( fmt );<br> ost=
<< std::setw(width) << std::setfill(fill)<br>  =
; << std::setprecision(precision)<br> &n=
bsp; << val;<br> return ost.str();<br>}<br><br><br>std::s=
tring tostring( int val,<br> int width=3D 0,<br>&n=
bsp; char fill=3D ' ',<br> int =
base=3D 10,<br> std::ios_base::fmtflags fmt=
=3D std::ostringstream().flags() )<br>{<br> std::ostringstream =
ost;<br> ost.setf( fmt );<br> ost << std::setw(width) <=
;< std::setfill(fill)<br> << std::se=
tbase(base)<br> << val;<br> retur=
n ost.str();<br>}<br><br>int main()<br>{<br> using namespace std;<br>=
<br> double my_e=3D -2.7182818284590452354;<br> in=
t my_i=3D -314159265;<br><br> cout << "=
(1)" << tostring( my_e ) << "end\n";<br> cout << "(=
2)" << tostring( my_e, 12 ) << "end\n";<br> cout <<=
"(3)" << tostring( my_e, 12, 12, ' ' ) << "end\n";<br> c=
out << "(4)" << tostring( my_e, 12, 6, '0' ) << "en=
d\n";<br> cout << "(5)" << tostring( my_e, 16, 6, '=
',<br>  =
; ios_base::scientific | ios_base::left )<br> =
<< "end\n";<br> cout << "(6)" <<=
tostring( my_e, 12, 6, ' ', ios_base::internal )<br> &nbs=
p; << "end\n";<br><br> cout << "\n(7)" =
<< tostring( my_i ) << "end\n";<br> cout << "(8)" &=
lt;< tostring( my_i, 12, ';' ) << "end\n";<br> cout <<=
"(9)" << tostring( my_i, 12, '0', 10, ios_base::internal )<br> =
<< "end\n";<br> cout << "(=
A)" << tostring(-my_i, 12, ' ', 16, ios_base::showbase )<br> &nb=
sp; << "end\n";<br><br>/* Yields:<br>(1)-2.71=
828end<br>(2) -2.71828end<br>(3)-2.71828182846end<br>(4)0=
000-2.71828end<br>(5)-2.718282e+00 end<br>(6)-  =
; 2.71828end<br><br>(7)-314159265end<br>(8);;-314159265end<br>(9)-003141592=
65end<br>(A) 0x12b9b0a1end<br>*/<br><br>}<br><br>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
<br />
<br />
------=_Part_365_12941967.1359991660879--
.