C++ Standard Input/Output -- <iostream>
<iostream>The library is a header file in the C++ standard library used for input and output operations.
<iostream> defines several commonly used stream classes and operators, allowing programs to interact with standard input and output devices (such as the keyboard and screen).
The following are<iostream>Detailed usage instructions for the library, including its main classes and common usage examples.
Main Classes
std::istream: Abstract base class for input operations.std::ostream: Abstract base class for output operations.std::iostream: Inherited fromstd::istreamandstd::ostream, used for both input and output operations.std::cin: standard input stream object, usually associated with the keyboard.std::cout: standard output stream object, usually associated with the screen.std::cerr: standard error output stream object, unbuffered, usually associated with the screen.std::clog: standard log stream object, buffered, usually associated with the screen.
Common Operators
>>: input operator, reads data from the input stream.<<: output operator, writes data to the output stream.
Basic Usage
Standard input and output:
Example
int main() {
int age;
std::string name;
// Use std::cout to output to the screen
std::cout << "Enter your name: ";
// Use std::cin to read input from the keyboard
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
// Output the read data
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;
return 0;
}
Standard error output:
Example
int main() {
std::cerr << "An error occurred!" << std::endl;
return 0;
}
Standard log output:
Example
int main() {
std::clog << "This is a log message." << std::endl;
return 0;
}
Formatted output:
Usage<iomanip>The library can format output, such as setting width, precision, and alignment.
Example
#include <iomanip>
int main() {
double pi = 3.14159;
// Set output precision
std::cout << std::setprecision(3) << pi << std::endl;
// Set output width and alignment
std::cout << std::setw(10) << std::left << pi << std::endl;
std::cout << std::setw(10) << std::right << pi << std::endl;
return 0;
}
Stream status check:
You can check the state of the input and output streams to determine whether an operation was successful.
Example
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
// Check if the input operation was successful
if (std::cin.fail()) {
std::cerr << "Invalid input!" << std::endl;
} else {
std::cout << "You entered: " << num << std::endl;
}
return 0;
}
Handling string input
Usagestd::getlineThe function can read an entire line of input that contains spaces.
Example
#include <string>
int main() {
std::string fullName;
std::cout << "Enter your full name: ";
std::getline(std::cin, fullName);
std::cout << "Hello, " << fullName << "!" << std::endl;
return 0;
}
The above examples demonstrate<iostream>Basic usage and common operations of the library, helping you handle input and output in C++ programs.