Topic: Adding more to_string overloads for collections (and


Author: ericcurtin17@gmail.com
Date: Sun, 26 Aug 2018 06:04:25 -0700 (PDT)
Raw View
------=_Part_636_1485232435.1535288665271
Content-Type: multipart/alternative;
 boundary="----=_Part_637_607643800.1535288665272"

------=_Part_637_607643800.1535288665272
Content-Type: text/plain; charset="UTF-8"

Would be particularly helpful, when you want to put all of the contents of
a collection in a std::string without all the boilerplate. Some example
test code;

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>

using namespace std;

string to_string(const string& val) {
  return val;
}

string to_string(const char val) {
  return string(1, val);
}

template <typename T>
string to_string(const vector<T>& val) {
  string ret = "{";
  for (const auto& v : val) {
    ret += to_string(v) + ", ";
  }

  ret = ret.substr(0, ret.size() - 2);
  ret += "}";

  return ret;
}

template <typename T>
string to_string(const set<T>& val) {
  string ret = "{";
  for (const auto& v : val) {
    ret += to_string(v) + ", ";
  }

  ret = ret.substr(0, ret.size() - 2);
  ret += "}";

  return ret;
}

template <typename k, typename v>
string to_string(const map<k, v>& val) {
  string ret = "{";
  for (const auto& kv : val) {
    ret += '{' + to_string(kv.first) + ", " + to_string(kv.second) + "}, ";
  }

  ret = ret.substr(0, ret.size() - 2);
  ret += "}";

  return ret;
}

int main() {
  string a = "asdasd";
  cout << to_string(a) << '\n';

  vector<string> b = {"djassda", "dsajkh"};
  cout << to_string(b) << '\n';

  vector<int> c = {1, 2};
  cout << to_string(c) << '\n';

  vector<char> d = {'1', 'd'};
  cout << to_string(d) << '\n';

  map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
  cout << to_string(m) << '\n';

  set<string> s = {"John", "Kelly", "Amanda", "Kim"};
  cout << to_string(s) << '\n';

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/ab8bd29c-e2b4-4f9a-88dc-ff6b2f222360%40isocpp.org.

------=_Part_637_607643800.1535288665272
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Would be particularly helpful, when you want to put a=
ll of the contents of a collection in a std::string without all the boilerp=
late. Some example test code;</div><div><br></div><div>#include &lt;iostrea=
m&gt;</div><div>#include &lt;map&gt;</div><div>#include &lt;string&gt;</div=
><div>#include &lt;vector&gt;</div><div>#include &lt;set&gt;</div><div><br>=
</div><div>using namespace std;</div><div><br></div><div>string to_string(c=
onst string&amp; val) {</div><div>=C2=A0 return val;</div><div>}</div><div>=
<br></div><div>string to_string(const char val) {</div><div>=C2=A0 return s=
tring(1, val);</div><div>}</div><div><br></div><div>template &lt;typename T=
&gt;</div><div>string to_string(const vector&lt;T&gt;&amp; val) {</div><div=
>=C2=A0 string ret =3D &quot;{&quot;;</div><div>=C2=A0 for (const auto&amp;=
 v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + &quot;, &quot;;=
</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =3D ret.substr(0, r=
et.size() - 2);</div><div>=C2=A0 ret +=3D &quot;}&quot;;</div><div><br></di=
v><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>template &lt=
;typename T&gt;</div><div>string to_string(const set&lt;T&gt;&amp; val) {</=
div><div>=C2=A0 string ret =3D &quot;{&quot;;</div><div>=C2=A0 for (const a=
uto&amp; v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + &quot;,=
 &quot;;</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =3D ret.sub=
str(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D &quot;}&quot;;</div><div>=
<br></div><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>temp=
late &lt;typename k, typename v&gt;</div><div>string to_string(const map&lt=
;k, v&gt;&amp; val) {</div><div>=C2=A0 string ret =3D &quot;{&quot;;</div><=
div>=C2=A0 for (const auto&amp; kv : val) {</div><div>=C2=A0 =C2=A0 ret +=
=3D &#39;{&#39; + to_string(kv.first) + &quot;, &quot; + to_string(kv.secon=
d) + &quot;}, &quot;;</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 re=
t =3D ret.substr(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D &quot;}&quot=
;;</div><div><br></div><div>=C2=A0 return ret;</div><div>}</div><div><br></=
div><div>int main() {</div><div>=C2=A0 string a =3D &quot;asdasd&quot;;</di=
v><div>=C2=A0 cout &lt;&lt; to_string(a) &lt;&lt; &#39;\n&#39;;</div><div><=
br></div><div>=C2=A0 vector&lt;string&gt; b =3D {&quot;djassda&quot;, &quot=
;dsajkh&quot;};</div><div>=C2=A0 cout &lt;&lt; to_string(b) &lt;&lt; &#39;\=
n&#39;;</div><div><br></div><div>=C2=A0 vector&lt;int&gt; c =3D {1, 2};</di=
v><div>=C2=A0 cout &lt;&lt; to_string(c) &lt;&lt; &#39;\n&#39;;</div><div><=
br></div><div>=C2=A0 vector&lt;char&gt; d =3D {&#39;1&#39;, &#39;d&#39;};</=
div><div>=C2=A0 cout &lt;&lt; to_string(d) &lt;&lt; &#39;\n&#39;;</div><div=
><br></div><div>=C2=A0 map&lt;int, char&gt; m =3D {{1, &#39;a&#39;}, {3, &#=
39;b&#39;}, {5, &#39;c&#39;}, {7, &#39;d&#39;}};</div><div>=C2=A0 cout &lt;=
&lt; to_string(m) &lt;&lt; &#39;\n&#39;;</div><div><br></div><div>=C2=A0 se=
t&lt;string&gt; s =3D {&quot;John&quot;, &quot;Kelly&quot;, &quot;Amanda&qu=
ot;, &quot;Kim&quot;};</div><div>=C2=A0 cout &lt;&lt; to_string(s) &lt;&lt;=
 &#39;\n&#39;;</div></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/ab8bd29c-e2b4-4f9a-88dc-ff6b2f222360%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/ab8bd29c-e2b4-4f9a-88dc-ff6b2f222360=
%40isocpp.org</a>.<br />

------=_Part_637_607643800.1535288665272--

------=_Part_636_1485232435.1535288665271--

.