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 <iostream=
></div><div>#include <map></div><div>#include <string></div>=
<div>#include <vector></div><div>#include <set></div><div><br><=
/div><div>using namespace std;</div><div><br></div><div>string to_string(co=
nst string& 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 <typename T&=
gt;</div><div>string to_string(const vector<T>& val) {</div><div>=
=C2=A0 string ret =3D "{";</div><div>=C2=A0 for (const auto& =
v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + ", ";<=
/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 "}";</div><div><br></div=
><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>template <=
typename T></div><div>string to_string(const set<T>& val) {</d=
iv><div>=C2=A0 string ret =3D "{";</div><div>=C2=A0 for (const au=
to& v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + ", =
";</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 "}";</div><div><=
br></div><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>templ=
ate <typename k, typename v></div><div>string to_string(const map<=
k, v>& val) {</div><div>=C2=A0 string ret =3D "{";</div><d=
iv>=C2=A0 for (const auto& kv : val) {</div><div>=C2=A0 =C2=A0 ret +=3D=
'{' + to_string(kv.first) + ", " + to_string(kv.second) =
+ "}, ";</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 "}";=
</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 "asdasd";</div>=
<div>=C2=A0 cout << to_string(a) << '\n';</div><div><br=
></div><div>=C2=A0 vector<string> b =3D {"djassda", "d=
sajkh"};</div><div>=C2=A0 cout << to_string(b) << '\n&=
#39;;</div><div><br></div><div>=C2=A0 vector<int> c =3D {1, 2};</div>=
<div>=C2=A0 cout << to_string(c) << '\n';</div><div><br=
></div><div>=C2=A0 vector<char> d =3D {'1', 'd'};</di=
v><div>=C2=A0 cout << to_string(d) << '\n';</div><div><=
br></div><div>=C2=A0 map<int, char> m =3D {{1, 'a'}, {3, '=
;b'}, {5, 'c'}, {7, 'd'}};</div><div>=C2=A0 cout <&l=
t; to_string(m) << '\n';</div><div><br></div><div>=C2=A0 set&=
lt;string> s =3D {"John", "Kelly", "Amanda"=
;, "Kim"};</div><div>=C2=A0 cout << to_string(s) << &=
#39;\n';</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" 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--
.