Guest User

Untitled

a guest
Oct 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. /*
  2. * 4th's C++ Tutorials
  3. * Templates: First Timers
  4. *
  5. * Funtion template basics
  6. * Class template basics
  7. * Function template deduction
  8. * Class template defaults
  9. *
  10. */
  11.  
  12. #include <iostream>
  13. #include <string>
  14. #include <vector>
  15. #include <map>
  16.  
  17. void wait(){
  18. std::cout << "\nPress any key to continue...";
  19. std::cin.get();
  20. }
  21.  
  22. // making a function template
  23. template<class T>
  24. T sum(T n1, T n2){
  25. return n1+n2;
  26. }
  27.  
  28. // making a class template
  29. template<class T>
  30. class Storage{
  31. T obj;
  32. public:
  33. Storage(){}
  34. Storage(const T& n):obj(n){}
  35. Storage(T&& n):obj(n){}
  36. Storage(const Storage& o):obj(o.obj){}
  37. Storage(Storage&& o):obj(o.obj){}
  38.  
  39. void Set(const T& newobj){
  40. obj = newobj;
  41. }
  42.  
  43. T Get(){
  44. return obj;
  45. }
  46.  
  47. bool Equal(const Storage& s) const{
  48. return (obj == s.obj);
  49. }
  50.  
  51. bool operator ==(const Storage& s) const{
  52. return Equal(s);
  53. }
  54. };
  55.  
  56. // function template arguments
  57. template<class Tr, class T1, class T2>
  58. Tr _sum(T1 n1, T2 n2){
  59. return n1+n2;
  60. }
  61.  
  62. // class template arguments
  63. template<class A, class B = int, class C = Storage<A> >
  64. /*
  65. typedef double A;
  66. typedef int B;
  67. typedef Storage<A> C;
  68. */
  69. class StorageClassifier{
  70. struct Classification{
  71. public:
  72. std::vector<C> objs;
  73. };
  74.  
  75. std::map<B,Classification> Classes;
  76. public:
  77. StorageClassifier(){
  78. }
  79.  
  80. void Classify(const A& obj, const B& type){
  81. Classes[type].objs.push_back(C(obj));
  82. }
  83.  
  84. bool IsClassified(const A& obj, const B& type) const{
  85. auto & vec = Classes.find(type)->second.objs;
  86. for (auto it = vec.begin(); it != vec.end(); ++it){
  87. if (it->operator==(obj)) {return true;}
  88. }
  89. return false;
  90. }
  91. };
  92.  
  93. std::string BoolString(bool b){
  94. if (b) return "true"; return "false";
  95. }
  96.  
  97. void ClassificationCheck(const StorageClassifier<double>& classifier, const char* nn, double n){
  98. std::cout << "\n" << nn << ":"
  99. << "\n0: " << BoolString(classifier.IsClassified(n,0))
  100. << "\n1: " << BoolString(classifier.IsClassified(n,1))
  101. << "\n2: " << BoolString(classifier.IsClassified(n,2))
  102. << "\n3: " << BoolString(classifier.IsClassified(n,3))
  103. << "\n";
  104. }
  105.  
  106. int main(){
  107. std::cout << "float: " << sum<float>(.4f,10) << "\ninteger: " << sum<int>(5,10)
  108. << "\nstring: " << sum<std::string>("std::string's have"," a \"+\" operator");
  109.  
  110. wait();
  111.  
  112.  
  113. Storage<std::string> box1("A string stored in the Storage class tempalte");
  114. Storage<std::string> box2(box1);
  115. //Storage<int> box3(box1);
  116. Storage<Storage<std::string> > box4(Storage<std::string>("Even more silly than single item containers."));
  117.  
  118. std::cout << "\n" << box1.Get() << "\n" << box2.Get() << "\n" << box4.Get().Get();
  119. wait();
  120.  
  121. //std::cout << "\nfloat: " << sum(.6f,4) << "\ninteger: " << sum(5,10)
  122. // << "\nstring: " << sum("Type's can get you ","so watch out");
  123.  
  124. int a = 20;
  125. float b = 0.53612f;
  126.  
  127. auto c = _sum<float>(a,b);
  128.  
  129. std::cout << "\n" << c;
  130. wait();
  131. std::cout << "\n";
  132.  
  133. StorageClassifier<double> classifier1;
  134.  
  135. classifier1.Classify(2.5,1);
  136. classifier1.Classify(2.5,2);
  137.  
  138. classifier1.Classify(5,1);
  139. classifier1.Classify(5,3);
  140.  
  141. classifier1.Classify(-9.3,0);
  142. classifier1.Classify(-9.3,2);
  143.  
  144. classifier1.Classify(-100,0);
  145. classifier1.Classify(-100,3);
  146.  
  147. ClassificationCheck(classifier1,"2.5",2.5);
  148. ClassificationCheck(classifier1,"5",5);
  149. ClassificationCheck(classifier1,"-9.3",-9.3);
  150. ClassificationCheck(classifier1,"-100",-100);
  151.  
  152. wait();
  153. return 0;
  154. }
Add Comment
Please, Sign In to add comment