fork download
  1. # include <bits/stdc++.h>
  2. using namespace std;
  3. int Hash(int x) {
  4. return x;
  5. }
  6. // Forward declarations
  7. template <typename T, typename C> int Hash(pair <T, C> x);
  8. template <typename T> ostream& operator<<(ostream &os, vector <T> x);
  9. template <typename T, typename C> ostream& operator<< (ostream &os, pair<T, C> x);
  10. template <typename T, typename C> ostream & Output (ostream &os, pair<T, C> x);
  11.  
  12. template <typename T> int Hash(vector<T> x) {
  13. int ans = 0;
  14. for (auto c : x)
  15. ans = 2 * ans + Hash(c);//Fails if c is a pair: no matching function for call to ‘Hash(std::pair<int, int>&)
  16. return ans;
  17. }
  18.  
  19. template <typename T, typename C> auto Hash(pair <T, C> x) -> int {
  20. return Hash(x.first) + Hash(x.second) * 14;
  21. }
  22. template <typename T>
  23. ostream &operator<<(ostream &os, vector <T> x)
  24. {
  25. os << "{";
  26. int cou = 0;
  27. for (auto c : x) {
  28. if (cou++) os << ", ";
  29. os << c;//Works even if c is a pair despite calling operator declared late
  30. }
  31. return os << "}";
  32. }
  33. template <typename T, typename C> ostream & operator<< (ostream &os, pair<T, C> x) {
  34. return os << "<" << x.first << ", " << x.second << ">";
  35. }
  36. ostream & Output(ostream &os, const char * x) {
  37. return os << x;
  38. }
  39. ostream & Output(ostream &os, int x) {
  40. return os << x;
  41. }
  42. template <typename T>
  43. ostream &Output(ostream &os, vector <T> x)
  44. {
  45. Output(os, "{");
  46. int cou = 0;
  47. for (auto c : x) {
  48. if (cou++) Output(os, ", ");
  49. Output(os, c);//Fails if c is a pair: no matching function for call to ‘Output(std::ostream&, std::pair<int, int>&)
  50. }
  51. return Output(os, "}");
  52. }
  53. template <typename T, typename C> ostream & Output (ostream &os, pair<T, C> x) {
  54. return Output(Output(Output(Output(Output(os, "<"), x.first), ", "), x.second), ">");
  55. }
  56. int main() {
  57. vector <pair <int, int> > x;
  58. x.emplace_back(13, 10);
  59. x.emplace_back(1, 16);
  60. cout << x << endl;//Compiles and outputs {<13, 10>, <1, 16>}
  61. cout << Hash(x) << endl;//no matching function for call to ‘Hash(std::pair<int, int>&) ans = 2 * ans + Hash(c);
  62. Output(cout, x);//no matching function for call to ‘Output(std::ostream&, std::pair<int, int>&) Output(os, c);
  63. }
Success #stdin #stdout 0s 4332KB
stdin
Standard input is empty
stdout
{<13, 10>, <1, 16>}
531
{<13, 10>, <1, 16>}