Handling standard input/output streams

In competitive programming, correctly handling the standard input and output streams is crucial for writing efficient and reliable code. The standard input stream, often referred to as stdin, is responsible for reading input data, while the standard output stream, known as stdout, is used to display the program's output.

To handle these streams in C++, the <iostream> header is included, which provides objects like std::cin and std::cout for reading and writing, respectively.

Reading input

To read input from the user or from a file, you can use std::cin. It's generally recommended to specify the type of data you want to read, such as integers, doubles, characters, strings, etc., for efficiency and to avoid potential errors.

Here's an example of reading an integer input:

int num;
std::cin >> num;

Similarly, you can read other types of data like this:

double dec;
std::cin >> dec;

char letter;
std::cin >> letter;

std::string name;
std::cin >> name;

If you need to read multiple inputs of the same type on a single line, you can use a loop to iterate over each input. For instance, if the inputs are space-separated integers:

int num;
while (std::cin >> num) {
    // Process the input
}

Writing output

To display output using the standard output stream, you can use std::cout. It works similarly to std::cin for reading.

Here's an example of writing an integer output:

int num = 50;
std::cout << num << std::endl;

You can write other types of data like this:

double dec = 3.14;
std::cout << dec << std::endl;

char letter = 'A';
std::cout << letter << std::endl;

std::string name = "John Doe";
std::cout << name << std::endl;

Additionally, you can use the std::endl manipulator to insert a new line after the output. Alternatively, you can use the escape character '\n' for the same purpose.

Handling file I/O

Competitive programming often involves reading input from and writing output to files, which can provide faster input/output operations. To handle file I/O in C++, you need to open the file using std::freopen and then use std::cin and std::cout as usual.

Here's an example of reading from a file named "input.txt" and writing to a file named "output.txt":

std::freopen("input.txt", "r", stdin);  // Opens input.txt for reading
std::freopen("output.txt", "w", stdout);  // Opens output.txt for writing

// Use std::cin to read input and std::cout to write output

std::fclose(stdin);  // Closes the input.txt file
std::fclose(stdout);  // Closes the output.txt file

By using file I/O, you can test your code with different test cases saved in separate files, which is essential for competitive programming.

Conclusion

Understanding how to handle standard input and output streams in C++ is crucial for competitive programming. By effectively utilizing std::cin and std::cout, you can efficiently read input, display output, and handle file I/O. Make sure to practice these techniques to become proficient in handling standard streams.


noob to master © copyleft