All efficient input taking and fast I/O techniques in C++ || A complete input/output C++ guide for competitive programming.

M M
Analytics Vidhya
Published in
4 min readJul 7, 2020
download on GitHub , HD image and PDF versions (link)

Getting basic knowledge

#include<iostream> must be included , without iostream I/O operations cant be performed. Including iostream automatically includes

<stream> <onstream> <ios> <iosfwd> <streambuf>

C++ input and output is performed in the form of streams or sequence of bytes. Both istream and ostream are under ios.

https://www.cplusplus.com/img/iostream.gif

Standard Input Stream(CIN):

  • It’s an predefined object or an instance of an istream class.
  • Istream is connected to the input devise i.e., keyboard to take input.
  • Here stream extraction(>>) operator is used to extract sequence of bytes.

Standard Output Stream(COUT):

  • It’s an predefined object or an instance of an ostream class.
  • ostream is connected to the output devise i.e., display screen.
  • Here stream insertion(<<) operator is used to insert the data.
basic way of input taking

Declare a variable with appropriate data type, take input and perform operations.

FAST I/O

In competitive programming it is important to read input as fast as possible and operations has to be performed faster there performance matters.

We can make the fast one even faster. How…?

by adding these few lines into your code. explanation 👇

std::ios::sync_with_stdio(false);

C++ iostream standard streams with their corresponding standard C streams are Synchronized . By adding ios_base::sync_with_stdio (false); which is true by default.

It avoids synchronization.If you disable the synchronization, then C++ streams are allowed to have their own independent buffers.

std::cin.tie(NULL);

Simply it unties cin from cout which means output is flushed/displayed on the console only on demand or when the buffer is full.(AVOIDS FLUSHING)

Example:

first understand the buzzwords.

  • Buffer -> simply buffer is a temporary placeholder, operations are performed faster.
  • Flushing -> storing buffered data to permanent memory.

Buffer flush or flushing the buffer ->buffer means storing temporarily+ flushing means saving permanently.

Ex:

  • In facebook if we are uploading a pic we select the pic, we describe, we tag , if we click on POST it posts until its stores on temporary placeholder.
  • In editing pics also i.e., until we click on save the changes we performed Are stored in …………

Instead of using endl; use “\n” why because it cause flushing.

ALL INPUT TAKING TECHNIQUES

1. Range base input taking

int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int range,result=0,temp;
std::cin>>range;
while (range--){
std::cin>>temp;
result+=temp;
}
std::cout<<result;

return 0;
}

Here range is taken as input and in while loop Until zero it take inputs.

2. Unknown no. of inputs taking until “\n”

int main(){
int result=0; std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
do {
int a;
std::cin>>a;
result+=a;
}while (std::cin&&std::cin.peek()!='\n');
std::cout<<result;
return 0;
}

.peek() Returns the next character in the input sequence. By using that program is terminated.

3. No. of test cases are given

int main(){    std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int TC,a,b;

std::cin>>TC;
while (TC--){
std::cin>>a>>b;
std::cout<<a+b<<"\n";
}
return 0;
}

4. Until both integers are zero

int main(){    int a=0,b=0; 
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
while((cin>>a>>b),a||b){
cout<<a+b<<"\n";
}
return 0;
}

5. EOF/Unknown test cases

int main(){
int a,b; std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
while (std::cin>>a>>b){//cin fails to takes input
// if its a non-numertic value
//why becoz.. a and b are int type
//while (scanf("%d %d",&a,&b)!=EOF){
// terminating by EOF signal
//while (scanf("%d %d",&a,&b)==2){
std::cout<<a+b<<"\n";
}
return 0;
}

6. Output with case no.

Case [NUMBER]: [ANSWER]

int main(){
int a,b,c=1; std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
while (std::cin>>a>>b){
if(c>1) std::cout<<"\n";
std::cout<<"Case "<<c++<<": "<<a+b;
}
return 0;
}

7. Variable Number of Inputs

For each input line “K” is taken, and taking K no. of inputs in same line.

int main(){
int k,a,sum=0;std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
while (std::cin>>k){
sum=0;
while (k--){
std::cin>>a;
sum+=a;
} std::cout<<sum<<"\n";
}
return 0;
}

Thank you..!

--

--