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 <iostrea=
m></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(c=
onst 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 s=
tring(1, val);</div><div>}</div><div><br></div><div>template <typename T=
></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, r=
et.size() - 2);</div><div>=C2=A0 ret +=3D "}";</div><div><br></di=
v><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>template <=
;typename T></div><div>string to_string(const set<T>& val) {</=
div><div>=C2=A0 string ret =3D "{";</div><div>=C2=A0 for (const a=
uto& 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.sub=
str(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>temp=
late <typename k, typename v></div><div>string to_string(const map<=
;k, v>& val) {</div><div>=C2=A0 string ret =3D "{";</div><=
div>=C2=A0 for (const auto& kv : val) {</div><div>=C2=A0 =C2=A0 ret +=
=3D '{' + to_string(kv.first) + ", " + to_string(kv.secon=
d) + "}, ";</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 "}"=
;;</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 "asdasd";</di=
v><div>=C2=A0 cout << to_string(a) << '\n';</div><div><=
br></div><div>=C2=A0 vector<string> b =3D {"djassda", "=
;dsajkh"};</div><div>=C2=A0 cout << to_string(b) << '\=
n';</div><div><br></div><div>=C2=A0 vector<int> c =3D {1, 2};</di=
v><div>=C2=A0 cout << to_string(c) << '\n';</div><div><=
br></div><div>=C2=A0 vector<char> d =3D {'1', 'd'};</=
div><div>=C2=A0 cout << to_string(d) << '\n';</div><div=
><br></div><div>=C2=A0 map<int, char> m =3D {{1, 'a'}, {3, &#=
39;b'}, {5, 'c'}, {7, 'd'}};</div><div>=C2=A0 cout <=
< to_string(m) << '\n';</div><div><br></div><div>=C2=A0 se=
t<string> s =3D {"John", "Kelly", "Amanda&qu=
ot;, "Kim"};</div><div>=C2=A0 cout << to_string(s) <<=
'\n';</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/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--
.