Skip to content

Instantly share code, notes, and snippets.

@keeproll
Created August 6, 2018 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeproll/b3ecc1607adc6a61dac11f926964ee14 to your computer and use it in GitHub Desktop.
Save keeproll/b3ecc1607adc6a61dac11f926964ee14 to your computer and use it in GitHub Desktop.
HackerRank - C++ Class Templates
#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;
}
@kumari-arya
Copy link

it's showing time limit out

@ppundeer
Copy link

add this method
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin

@sunny-sk
Copy link

sunny-sk commented Feb 2, 2021

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

@smallfoot47
Copy link

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

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

@satyam0804
Copy link

add this method
#define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin

can you tell me where to add this method ??

@0mehedihasan
Copy link

#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;
}

@SairamReddy96
Copy link

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 !

@Gaurav909565
Copy link

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

@smallfoot47
Copy link

smallfoot47 commented Feb 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment