Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 4th's C++ Tutorials
- * Templates: First Timers
- *
- * Funtion template basics
- * Class template basics
- * Function template deduction
- * Class template defaults
- *
- */
- #include <iostream>
- #include <string>
- #include <vector>
- #include <map>
- void wait(){
- std::cout << "\nPress any key to continue...";
- std::cin.get();
- }
- // making a function template
- template<class T>
- T sum(T n1, T n2){
- return n1+n2;
- }
- // making a class template
- template<class T>
- class Storage{
- T obj;
- public:
- Storage(){}
- Storage(const T& n):obj(n){}
- Storage(T&& n):obj(n){}
- Storage(const Storage& o):obj(o.obj){}
- Storage(Storage&& o):obj(o.obj){}
- void Set(const T& newobj){
- obj = newobj;
- }
- T Get(){
- return obj;
- }
- bool Equal(const Storage& s) const{
- return (obj == s.obj);
- }
- bool operator ==(const Storage& s) const{
- return Equal(s);
- }
- };
- // function template arguments
- template<class Tr, class T1, class T2>
- Tr _sum(T1 n1, T2 n2){
- return n1+n2;
- }
- // class template arguments
- template<class A, class B = int, class C = Storage<A> >
- /*
- typedef double A;
- typedef int B;
- typedef Storage<A> C;
- */
- class StorageClassifier{
- struct Classification{
- public:
- std::vector<C> objs;
- };
- std::map<B,Classification> Classes;
- public:
- StorageClassifier(){
- }
- void Classify(const A& obj, const B& type){
- Classes[type].objs.push_back(C(obj));
- }
- bool IsClassified(const A& obj, const B& type) const{
- auto & vec = Classes.find(type)->second.objs;
- for (auto it = vec.begin(); it != vec.end(); ++it){
- if (it->operator==(obj)) {return true;}
- }
- return false;
- }
- };
- std::string BoolString(bool b){
- if (b) return "true"; return "false";
- }
- void ClassificationCheck(const StorageClassifier<double>& classifier, const char* nn, double n){
- std::cout << "\n" << nn << ":"
- << "\n0: " << BoolString(classifier.IsClassified(n,0))
- << "\n1: " << BoolString(classifier.IsClassified(n,1))
- << "\n2: " << BoolString(classifier.IsClassified(n,2))
- << "\n3: " << BoolString(classifier.IsClassified(n,3))
- << "\n";
- }
- int main(){
- std::cout << "float: " << sum<float>(.4f,10) << "\ninteger: " << sum<int>(5,10)
- << "\nstring: " << sum<std::string>("std::string's have"," a \"+\" operator");
- wait();
- Storage<std::string> box1("A string stored in the Storage class tempalte");
- Storage<std::string> box2(box1);
- //Storage<int> box3(box1);
- Storage<Storage<std::string> > box4(Storage<std::string>("Even more silly than single item containers."));
- std::cout << "\n" << box1.Get() << "\n" << box2.Get() << "\n" << box4.Get().Get();
- wait();
- //std::cout << "\nfloat: " << sum(.6f,4) << "\ninteger: " << sum(5,10)
- // << "\nstring: " << sum("Type's can get you ","so watch out");
- int a = 20;
- float b = 0.53612f;
- auto c = _sum<float>(a,b);
- std::cout << "\n" << c;
- wait();
- std::cout << "\n";
- StorageClassifier<double> classifier1;
- classifier1.Classify(2.5,1);
- classifier1.Classify(2.5,2);
- classifier1.Classify(5,1);
- classifier1.Classify(5,3);
- classifier1.Classify(-9.3,0);
- classifier1.Classify(-9.3,2);
- classifier1.Classify(-100,0);
- classifier1.Classify(-100,3);
- ClassificationCheck(classifier1,"2.5",2.5);
- ClassificationCheck(classifier1,"5",5);
- ClassificationCheck(classifier1,"-9.3",-9.3);
- ClassificationCheck(classifier1,"-100",-100);
- wait();
- return 0;
- }
Add Comment
Please, Sign In to add comment