Topic: Adding more to_string overloads for containers (and


Author: ericcurtin17@gmail.com
Date: Sun, 26 Aug 2018 06:35:21 -0700 (PDT)
Raw View
------=_Part_646_321201388.1535290521151
Content-Type: multipart/alternative;
 boundary="----=_Part_647_685927003.1535290521151"

------=_Part_647_685927003.1535290521151
Content-Type: text/plain; charset="UTF-8"

Would be particularly helpful, when you want to put all of the contents of
a container 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/76362bfa-a887-4429-b371-3fca7c43c6e8%40isocpp.org.

------=_Part_647_685927003.1535290521151
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 container in a std::string without all the boilerpl=
ate. Some example test code;</div><div><br></div><div>#include &lt;iostream=
&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(co=
nst 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 st=
ring(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, re=
t.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>template &lt;=
typename T&gt;</div><div>string to_string(const set&lt;T&gt;&amp; val) {</d=
iv><div>=C2=A0 string ret =3D &quot;{&quot;;</div><div>=C2=A0 for (const au=
to&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.subs=
tr(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>templ=
ate &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><d=
iv>=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.second) =
+ &quot;}, &quot;;</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =
=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></di=
v><div>int main() {</div><div>=C2=A0 string a =3D &quot;asdasd&quot;;</div>=
<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;d=
sajkh&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};</div>=
<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;};</di=
v><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;&l=
t; to_string(m) &lt;&lt; &#39;\n&#39;;</div><div><br></div><div>=C2=A0 set&=
lt;string&gt; s =3D {&quot;John&quot;, &quot;Kelly&quot;, &quot;Amanda&quot=
;, &quot;Kim&quot;};</div><div>=C2=A0 cout &lt;&lt; to_string(s) &lt;&lt; &=
#39;\n&#39;;</div><div>}</div><div><br></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/76362bfa-a887-4429-b371-3fca7c43c6e8%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8=
%40isocpp.org</a>.<br />

------=_Part_647_685927003.1535290521151--

------=_Part_646_321201388.1535290521151--

.