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 &lt;iostream&gt;<br>#include &lt;iomanip&gt;<=
br>#include &lt;sstream&gt;<br>#include &lt;string&gt;<br><br>std::string t=
ostring( double val,<br>&nbsp;&nbsp;&nbsp; &nbsp; int width=3D 0,<br>&nbsp;=
&nbsp;&nbsp; &nbsp; int precision=3D std::ostringstream().precision(),<br>&=
nbsp;&nbsp;&nbsp; &nbsp; char fill=3D ' ',<br>&nbsp;&nbsp;&nbsp; &nbsp; std=
::ios_base::fmtflags&nbsp; fmt=3D std::ostringstream().flags() )<br>{<br>&n=
bsp; std::ostringstream&nbsp; ost;<br>&nbsp; ost.setf( fmt );<br>&nbsp; ost=
 &lt;&lt; std::setw(width) &lt;&lt; std::setfill(fill)<br>&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp; &lt;&lt; std::setprecision(precision)<br>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp; &lt;&lt; val;<br>&nbsp; return ost.str();<br>}<br><br><br>std::s=
tring tostring( int val,<br>&nbsp;&nbsp;&nbsp; &nbsp; int width=3D 0,<br>&n=
bsp;&nbsp;&nbsp; &nbsp; char fill=3D ' ',<br>&nbsp;&nbsp;&nbsp; &nbsp; int =
base=3D 10,<br>&nbsp;&nbsp;&nbsp; &nbsp; std::ios_base::fmtflags&nbsp; fmt=
=3D std::ostringstream().flags() )<br>{<br>&nbsp; std::ostringstream&nbsp; =
ost;<br>&nbsp; ost.setf( fmt );<br>&nbsp; ost &lt;&lt; std::setw(width) &lt=
;&lt; std::setfill(fill)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; std::se=
tbase(base)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; val;<br>&nbsp; retur=
n ost.str();<br>}<br><br>int main()<br>{<br>&nbsp; using namespace std;<br>=
&nbsp; <br>&nbsp; double&nbsp; my_e=3D -2.7182818284590452354;<br>&nbsp; in=
t&nbsp;&nbsp;&nbsp;&nbsp; my_i=3D -314159265;<br><br>&nbsp; cout &lt;&lt; "=
(1)" &lt;&lt; tostring( my_e ) &lt;&lt; "end\n";<br>&nbsp; cout &lt;&lt; "(=
2)" &lt;&lt; tostring( my_e, 12 ) &lt;&lt; "end\n";<br>&nbsp; cout &lt;&lt;=
 "(3)" &lt;&lt; tostring( my_e, 12, 12, ' ' ) &lt;&lt; "end\n";<br>&nbsp; c=
out &lt;&lt; "(4)" &lt;&lt; tostring( my_e, 12,&nbsp; 6, '0' ) &lt;&lt; "en=
d\n";<br>&nbsp; cout &lt;&lt; "(5)" &lt;&lt; tostring( my_e, 16,&nbsp; 6, '=
 ',<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp=
;&nbsp;&nbsp; ios_base::scientific | ios_base::left )<br>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp; &lt;&lt; "end\n";<br>&nbsp; cout &lt;&lt; "(6)" &lt;&lt;=
 tostring( my_e, 12,&nbsp; 6, ' ', ios_base::internal )<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp; &lt;&lt; "end\n";<br><br>&nbsp; cout &lt;&lt; "\n(7)" =
&lt;&lt; tostring( my_i ) &lt;&lt; "end\n";<br>&nbsp; cout &lt;&lt; "(8)" &=
lt;&lt; tostring( my_i, 12, ';' ) &lt;&lt; "end\n";<br>&nbsp; cout &lt;&lt;=
 "(9)" &lt;&lt; tostring( my_i, 12, '0', 10, ios_base::internal )<br>&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; "end\n";<br>&nbsp; cout &lt;&lt; "(=
A)" &lt;&lt; tostring(-my_i, 12, ' ', 16, ios_base::showbase )<br>&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; "end\n";<br><br>/* Yields:<br>(1)-2.71=
828end<br>(2)&nbsp;&nbsp;&nbsp; -2.71828end<br>(3)-2.71828182846end<br>(4)0=
000-2.71828end<br>(5)-2.718282e+00&nbsp;&nbsp; end<br>(6)-&nbsp;&nbsp;&nbsp=
; 2.71828end<br><br>(7)-314159265end<br>(8);;-314159265end<br>(9)-003141592=
65end<br>(A)&nbsp; 0x12b9b0a1end<br>*/<br><br>}<br><br>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
&nbsp;<br />
&nbsp;<br />

------=_Part_365_12941967.1359991660879--

.