0

I want to design a universal print funtion, it can print "single type", like int double std::string, also "iteratable type", like vector and map.

here is one of my idea:

#include <bits/stdc++.h>
using namespace std;

void show(double b) {
  cout << b << endl;
}

template <typename T>
void show(const T& b) {
  for (const auto & i : b) cout << i << " ";
  cout << endl;
}

int main() {
  std::vector<int> a = {1, 2, 3, 5, 5}; 
  double b = 1;
  show(b);
  show(a);
}

it works well, can distinguish double and other iteratable type, but i think it is not elegant enough, because for every single type, it need to implement a function.

it will be good if i can judge the template class if it's iteratable.

could you help on this?

nick
  • 832
  • 3
  • 12

0 Answers0