One thing easy to miss about Python is how it can easily print anything, including lists of dictionaries of lists of objects. C++, not so much. So I wrote a simple template function to print a vector of printable types, and then one for deques.
template<class T>
void pr_vector(const vector <T> &v) {
for (auto i : v)
cout << i;
cout << "\n";
}
template<class T>
void pr_deque(const deque <T> &v) {
for (auto i : v)
cout << i;
cout << "\n";
}
Well, lots of duplication there. I tried templating on the container type too, template <class C, class T>, but the resulting C <T> didn't work; it probably should with some syntax, but I don't know what, yet.
But I figured I'd try auto:
void pr_cont(const auto &v) {
for (auto i : v)
cout << i;
cout << "\n";
}
...that subsumes both templates. And lots of others. vector, deque, list... Of course, the values themselves have to be printable, it can't handle a vector<pair<int, string>> because there's no printing on pairs. But still, for C, this is amazing!
template<class T>
void pr_vector(const vector <T> &v) {
for (auto i : v)
cout << i;
cout << "\n";
}
template<class T>
void pr_deque(const deque <T> &v) {
for (auto i : v)
cout << i;
cout << "\n";
}
Well, lots of duplication there. I tried templating on the container type too, template <class C, class T>, but the resulting C <T> didn't work; it probably should with some syntax, but I don't know what, yet.
But I figured I'd try auto:
void pr_cont(const auto &v) {
for (auto i : v)
cout << i;
cout << "\n";
}
...that subsumes both templates. And lots of others. vector, deque, list... Of course, the values themselves have to be printable, it can't handle a vector<pair<int, string>> because there's no printing on pairs. But still, for C, this is amazing!