C++ Class Templates

Sort by

recency

|

240 Discussions

|

  • + 0 comments

    As many have noticed, some test cases fail due to timeout when submitting under C++ 14 or C++ 20 but not when submitting for C++ 11.

    It has also been pointed out that disabling ios_base::sync_with_stdio at the top of main() resolves the issue. Disabling this syncronization "... the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases." - https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio.html

    I was curious why C++ 11 would be faster or not suffer from this issue, so I went poking around Compiler Explorer (https://godbolt.org/). I was not able to find any difference in various GCC versions and only minor differences with Clang when compiling with -std=c++11 vs -std=c++14 so long as the same compiler flags were used with each case. This leads me to wonder if different compiler flags are used when selecting C++ 11. Perhaps a different optimization levels? Another possibility is that the difference could be in more at a system level. If your compilation / execution task is offloaded to a different set of resources depending on the selected C++ version, then either system load or hardware performance could be to blame.

  • + 1 comment

    Had execution time problems with c++ v14 so I used v11 which worked fine. (using v14 random tests where passing on each submission)

    #include <iostream>
    using namespace std;
    
    template <class T>
    class AddElements
    {
    private:
        T element;
    
    public:
        AddElements(T element) : element(element) {}
        T add(T arg) { return element + arg; }
    };
    
    template <>
    class AddElements<string>
    {
    private:
        string element;
    
    public:
        AddElements(const string &element) : element(element) {}
        string concatenate(const string &arg) { return element + arg; }
    };
    
    int main()
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            string type;
            cin >> type;
            if (type == "float")
            {
                double element1, element2;
                cin >> element1 >> element2;
                AddElements<double> myfloat(element1);
                cout << myfloat.add(element2) << endl;
            }
            else if (type == "int")
            {
                int element1, element2;
                cin >> element1 >> element2;
                AddElements<int> myint(element1);
                cout << myint.add(element2) << endl;
            }
            else if (type == "string")
            {
                string element1, element2;
                cin >> element1 >> element2;
                AddElements<string> mystring(element1);
                cout << mystring.concatenate(element2) << endl;
            }
        }
        return 0;
    }
    
  • + 0 comments

    Well said! Class templates are incredibly powerful in C++, especially for creating reusable and type-safe components. Gurubhai247 com Login

  • + 0 comments

    Here is C++ class templates solution - https://programmingoneonone.com/hackerrank-cpp-class-templates-problem-solution.html

  • + 0 comments
    template <class T>
    class AddElements{
        private:
        T element;
        public:
        AddElements(const T& t) : element(t) {}
        T add(const T& t){ return element + t;}
        T concatenate(const T& t){ return element + t;}
        
    };