Common C++ I/O in ACM Mode
Personal notes, AI-generated.
Include the universal header first:
#include <bits/stdc++.h>
cin
cin is the standard input object in C++, part of the iostream library. It reads data from the standard input stream (typically the keyboard).
cin blocks program execution while waiting for user input. Specifically, it blocks in the following cases:
- Waiting for input: When the program reaches an input statement and there is not enough data in the standard input buffer,
cinblocks and waits for the user to type and press Enter. - Insufficient data in buffer: If the input buffer contains some data but not enough to satisfy the current read operation,
cinalso blocks and waits for more input.
When used as a boolean expression, cin reflects the state of the input operation:
- When input succeeds,
cinevaluates totrue. - When EOF is reached, or when the input type does not match the variable type (e.g., trying to read a letter into an integer variable),
cinevaluates tofalse.
cin, as the input stream object in the C++ standard library, can read a variety of primitive data types as well as some composite types defined in the library. The following are common data types that can be read directly with cin:
Primitive Data Types
- Integer types:
int,short,long,long long, and their unsigned variantsunsigned int,unsigned short,unsigned long,unsigned long long. - Floating-point types:
float,double,long double. - Character types:
char,unsigned char, andsigned char. - Boolean type:
bool.
Composite Types
- String:
std::string. When reading,cinskips any leading whitespace (spaces, newlines, etc.) and reads characters until the next whitespace character.
For most primitive types (such as int, double, char, etc.), when cin reads data using the >> operator, it automatically skips any leading whitespace including spaces, tabs, and newlines. This means if the beginning of the input buffer is whitespace, cin >> will ignore it and start reading from the first non-whitespace character.
getchar()
The getchar() function reads the next character from the input stream and consumes it — the character is removed from the input stream and is no longer available for subsequent input operations. Once getchar() reads a character, that character will not appear in the input stream again, regardless of whether cin, scanf, or any other function is used to read from standard input.
getline()
std::getline() is a function in the C++ standard library used to read a line of text from an input stream. It is primarily used with std::istream types (such as std::cin, file streams, etc.) and reads data from the given input stream until a newline character ('\n') is encountered. The content read (excluding the newline) is stored in a std::string or other character sequence.
std::getline() has several overloads. The two most commonly used signatures are:
-
Reading from standard input stream:
std::istream& getline(std::istream& is, std::string& str, char delim = '\n');is: The input stream (e.g.,std::cinor a file input stream).str: The string to store the line read from the input stream.delim: Optional parameter specifying the delimiter character (default is'\n'). The function reads until this character is encountered.
-
Reading from a string stream (from the
<sstream>header):std::istream& getline(std::stringstream& ss, std::string& str, char delim = '\n');- Similar to the above, but the input stream is a
std::stringstream.
- Similar to the above, but the input stream is a
std::getline() consumes (reads and removes) the delimiter from the input stream.
std::getline() does not skip whitespace. When reading a line from the input stream, it includes all characters in the line until the delimiter (default '\n') is encountered, but does not include the delimiter itself. This means spaces, tabs, and other whitespace characters are all read and stored as part of the string.
istringstream & stringstream
Both istringstream and stringstream are string stream classes provided by the C++ standard library. Both inherit from ios_base and provide read/write operations on in-memory strings. However, they differ in purpose and functionality.
istringstream
std::istringstreamis an input string stream, primarily used to read data from a string.- It supports only input operations — extracting (reading) data from a string, similar to how
std::cinreads from standard input or a file. - Use case: Parsing strings, converting string content to other data types (e.g., integers, floats).
stringstream
std::stringstreamsupports both input and output operations — it is a bidirectional string stream.- It can be used to read data from a string (input) as well as write data to a string (output).
std::stringstreamis more flexible and suitable for scenarios requiring both read and write operations on a string.- Use case: Reading string data, processing or formatting the data, then writing the result back to another string.
Usage Comparison
- When you only need to read data from a string,
std::istringstreamis a lighter-weight choice, as it is designed specifically for input. - When you need both read and write operations on a string,
std::stringstreamoffers greater flexibility and functionality.
Examples
istringstream example:
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string data = "123 45.67";
std::istringstream iss(data);
int intValue;
double doubleValue;
iss >> intValue >> doubleValue;
std::cout << intValue << ", " << doubleValue << std::endl; // Output: 123, 45.67
}
stringstream example:
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::stringstream ss;
ss << 123 << ' ' << 45.67; // Write data to ss
std::string data = ss.str(); // Get string from ss
std::cout << data << std::endl; // Output: 123 45.67
int intValue;
double doubleValue;
ss >> intValue >> doubleValue; // Read data (need to reset or clear ss first)
std::cout << intValue << ", " << doubleValue << std::endl;
}