-
-
Save keeproll/b3ecc1607adc6a61dac11f926964ee14 to your computer and use it in GitHub Desktop.
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <cassert> | |
using namespace std; | |
// Code Start =========================================== | |
template <class T> | |
class AddElements{ | |
T x; | |
public: | |
AddElements(T a){x=a;} | |
T add(T b){return x+b;} | |
}; | |
template<> | |
class AddElements<string>{ | |
string x; | |
public: | |
AddElements(string a){x=a;} | |
string concatenate(string b){return x+b;} | |
}; | |
// Code End ============================================= | |
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; | |
} |
add this method
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
add this method
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
can you explain or attach any reference to how it's work behind the scene
add this method
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cincan you explain or attach any reference to how it's work behind the scene
ios_base::sync_with_stdio(): https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio
cin.tie(): https://en.cppreference.com/w/cpp/io/basic_ios/tie
add this method
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
can you tell me where to add this method ??
#include
#include
#include
#include
#include
#include
using namespace std;
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
template
class AddElements{
T x;
public:
AddElements(T a){x=a;}
T add(T b){return x+b;}
};
template<>
class AddElements{
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 myfloat (element1);
cout << myfloat.add(element2) << endl;
}
else if(type == "int") {
int element1, element2;
cin >> element1 >> element2;
AddElements myint (element1);
cout << myint.add(element2) << endl;
}
else if(type == "string") {
string element1, element2;
cin >> element1 >> element2;
AddElements mystring (element1);
cout << mystring.concatenate(element2) << endl;
}
}
return 0;
}
Why did you write the same class again exclusively for strings?
We can directly concatenate them just like other data types right by just including #include !
add this method #define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
thank you so much for the code
can you please explain it
add this method #define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
thank you so much for the code can you please explain it
with #define, you replace every instance of 'cin' with 'ios_base::sync_with_stdio(false);cin.tie(NULL); cin'
- ios_base::sync_with_stdio(false) disables synchronization and allows C++ input/output streams to hold independent buffers, as opposed to using the same buffers as the underlying C streams.
- cin.tie(NULL) unties cin from cout. What this effectively means is that the buffers will no longer automatically flush between calls.
The improvement in performance is only a side-effect of these changes and won't always apply. The code snippet above uses cins and couts quite consistently and for the most part don't care about the buffers getting flushed, hence the improved performance.
Edit:typo
it's showing time limit out