为何我的HackerRank类模板代码在部分测试用例中出现TLE?
The Problem
I wrote code using basic templates and template specialization for a programming problem, but some test cases are hitting time limit exceeded (TLE). Here's my code:
template<class T> class AddElements{ T a; public: AddElements(T x) { a=x; } T add(T y) { return a+y; }; }; template<> class AddElements<string>{ string x; public: AddElements(string a) { x=a; } string concatenate(string b) { return x+b; }; }; int main () { int n,i; cin >> n; for(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; }
Why You're Getting TLE
Your template and specialization logic is totally correct—this timeout is almost certainly an input/output efficiency issue. By default:
cinandcoutare synchronized with C's stdio streams, which adds unnecessary overhead for large datasets.- Using
endlforces a full output buffer flush every time you print, which slows down repeated output operations.
The Fixes
Add these two lines at the very start of your main function to drastically speed up IO:
ios_base::sync_with_stdio(false); cin.tie(NULL);
Here's what they do:
ios_base::sync_with_stdio(false);: Disables synchronization between C++ streams and C stdio, cutting out redundant overhead.cin.tie(NULL);: Unlinkscinfromcout—this meanscinwon't automatically flushcoutevery time you read input, reducing the number of costly IO operations.
Another quick optimization: replace all instances of endl with '\n'. endl flushes the output buffer immediately, while '\n' just adds a newline and lets the buffer flush naturally when it's full. This saves a ton of time for test cases with lots of output.
Full Modified Code
#include <iostream> #include <string> using namespace std; template<class T> class AddElements{ T a; public: AddElements(T x) { a = x; } T add(T y) { return a + y; } }; template<> class AddElements<string>{ string x; public: AddElements(string a) { x = a; } string concatenate(string b) { return x + b; } }; int main () { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, i; cin >> n; for(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) << '\n'; } else if(type == "int") { int element1, element2; cin >> element1 >> element2; AddElements<int> myint(element1); cout << myint.add(element2) << '\n'; } else if(type == "string") { string element1, element2; cin >> element1 >> element2; AddElements<string> mystring(element1); cout << mystring.concatenate(element2) << '\n'; } } return 0; }
Extra Notes
Your core template logic is solid—this is purely an IO optimization problem. In competitive programming, these two IO tweaks are essential for handling large datasets without hitting TLE. They're so common that most C++ programmers add them to every code that deals with significant input/output.
内容的提问来源于stack exchange,提问作者Reetik Raj

