您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# C++中ostream的用法
## 1. 概述
`ostream`(输出流)是C++标准库中用于处理输出的基类,定义在`<iostream>`头文件中。作为`basic_ostream<char>`的typedef,它提供了格式化和非格式化的输出功能,是C++输入输出系统的核心组件之一。
## 2. 基本用法
### 2.1 标准输出
最常用的`ostream`对象是`std::cout`,通常与插入运算符`<<`配合使用:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
std::cout << "Integer: " << 42 << "\n"
<< "Float: " << 3.14 << std::endl;
return 0;
}
可以为自定义类型重载<<
运算符:
#include <iostream>
class Point {
public:
int x, y;
Point(int x, int y) : x(x), y(y) {}
};
std::ostream& operator<<(std::ostream& os, const Point& p) {
return os << "(" << p.x << ", " << p.y << ")";
}
int main() {
Point p(10, 20);
std::cout << p << std::endl; // 输出: (10, 20)
}
函数/操纵符 | 功能描述 | 示例 |
---|---|---|
setw(n) |
设置字段宽度 | cout << setw(10) << 42 |
setprecision(n) |
设置浮点数精度 | cout << setprecision(3) << 3.14159 |
fixed |
固定小数格式 | cout << fixed << 3.14 |
scientific |
科学计数法格式 | cout << scientific << 123.456 |
left /right |
左/右对齐 | cout << left << setw(10) << "Hi" |
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.141592653589793;
// 默认格式
std::cout << "Default: " << pi << std::endl;
// 固定小数,精度5
std::cout << "Fixed(5): " << std::fixed << std::setprecision(5) << pi << std::endl;
// 科学计数法,精度3
std::cout << "Scientific(3): " << std::scientific << std::setprecision(3) << pi << std::endl;
// 设置字段宽度和填充
std::cout << "Width(10): |" << std::setw(10) << 123 << "|" << std::endl;
std::cout << "Fill('*'): |" << std::setw(10) << std::setfill('*') << 123 << "|" << std::endl;
return 0;
}
ofstream
(输出文件流)继承自ostream
,可用于文件写入:
#include <fstream>
#include <string>
int main() {
std::ofstream outFile("output.txt");
if (outFile.is_open()) {
outFile << "This is line 1\n";
outFile << "This is line 2\n";
outFile.close();
} else {
std::cerr << "Unable to open file" << std::endl;
}
return 0;
}
ostringstream
允许将输出写入字符串:
#include <sstream>
#include <string>
int main() {
std::ostringstream oss;
oss << "The answer is " << 42;
std::string result = oss.str();
std::cout << result << std::endl; // 输出: The answer is 42
return 0;
}
函数 | 描述 |
---|---|
good() |
流状态正常 |
fail() |
发生非致命错误 |
bad() |
发生致命错误 |
eof() |
到达文件末尾 |
clear() |
重置错误状态 |
#include <iostream>
#include <fstream>
int main() {
std::ofstream outFile;
outFile.open("readonly.txt");
if (outFile.fail()) {
std::cerr << "Error opening file" << std::endl;
if (outFile.bad()) {
std::cerr << "Fatal error occurred" << std::endl;
}
return 1;
}
outFile << "Test";
outFile.close();
return 0;
}
可以设置不同的locale来影响格式化:
#include <iostream>
#include <locale>
int main() {
std::cout.imbue(std::locale("en_US.UTF-8"));
std::cout << 1234567.89 << std::endl; // 可能输出: 1,234,567.89
std::cout.imbue(std::locale("de_DE.UTF-8"));
std::cout << 1234567.89 << std::endl; // 可能输出: 1.234.567,89
return 0;
}
// 禁用同步提升性能(但不要与C stdio混用)
std::ios_base::sync_with_stdio(false);
ostream
作为C++标准输出系统的核心,提供了:
- 基本数据类型和自定义类型的输出能力
- 丰富的格式化控制选项
- 多种输出目标(控制台、文件、字符串等)
- 错误处理机制
掌握ostream
及其派生类的使用是C++开发者必备的基础技能,合理使用可以创建灵活、高效的输出系统。
“`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。