C++ Class Templates

  • + 1 comment

    Both my code and the top two codes in discussions are terminated due to timeout. I don't know how to reduce the runtime anymore. Could anyone successfully solving this problem point out the pitfalls in my code? The version 1 of the following code is my code. The version 2 is quoted from Discussions.

    // Version 1 - Phoenix(me)
    
    template<class T>
    class AddElements {
        T e1;
    public:
        AddElements (T arg) {e1=arg;}
        T add (T e2) {return e1 + e2;}
    };
    
    template <>
    class AddElements<string>{
    	string e1; 
    public:
    	AddElements (string str){e1 = str;}
    	string concatenate(string e2){return e1 + e2;}    
    };
    
    // Version 2 - kk65g3
    
    template <class T> class AddElements {
    public:
    	T element;
    	AddElements(T i) {element = i;}
    	T add(T i) {return element+i;}
    private:
    };
    
    template <> class AddElements <string> {
    public:
    	string element;
    	AddElements(string i) {element = i;}
    	string concatenate(string element2) {return element+element2;}
    private:
    };