当前位置: 首页 > news >正文

C++---cout、cerr、clog

在C++编程里,coutcerrclog是标准库提供的重要输出流对象,在数据输出方面发挥着关键作用。

一、cout:标准输出流

coutstd::ostream 类的对象,其作用是向标准输出设备(一般是控制台)输出数据。它和 C 语言中的 printf 函数类似,但 cout 具有类型安全和运算符重载的优势,使用起来更加方便。

1. 基本用法

借助 << 运算符,能把各种类型的数据输出到 cout

#include <iostream>
using namespace std;int main() {int num = 42;double pi = 3.14159;string name = "Alice";cout << "Hello, World!" << endl;cout << "Number: " << num << endl;cout << "Pi: " << pi << endl;cout << "Name: " << name << endl;return 0;
}

输出结果如下:

Hello, World!
Number: 42
Pi: 3.14159
Name: Alice
2. 格式化输出

通过操纵符(如 setwsetprecision 等)或者成员函数(像 width()precision()),可以对输出格式进行控制。

#include <iostream>
#include <iomanip>
using namespace std;int main() {double value = 123.456789;// 设置宽度和填充字符cout << setw(10) << setfill('*') << 42 << endl;  // 输出:********42// 设置精度cout << fixed << setprecision(3) << value << endl;  // 输出:123.457// 科学计数法cout << scientific << value << endl;  // 输出:1.234568e+02// 布尔值以文字形式输出cout << boolalpha << true << endl;  // 输出:truereturn 0;
}
3. 链式输出

<< 运算符返回的是对 cout 对象的引用,所以可以进行链式输出。

int a = 10, b = 20;
cout << "a = " << a << ", b = " << b << endl;  // 输出:a = 10, b = 20
4. 重定向输出

可以利用 rdbuf() 函数对 cout 的输出缓冲区进行重定向。

#include <iostream>
#include <fstream>
using namespace std;int main() {ofstream file("output.txt");streambuf* oldBuf = cout.rdbuf();  // 保存原来的缓冲区cout.rdbuf(file.rdbuf());  // 将输出重定向到文件cout << "This will be written to the file." << endl;cout.rdbuf(oldBuf);  // 恢复原来的输出cout << "This will be written to the console." << endl;file.close();return 0;
}

二、cerr:标准错误流

cerr 同样是 std::ostream 类的对象,它专门用于输出错误信息。和 cout 的主要区别在于,cerr 的输出是不经过缓冲的,会立即显示在终端上。

1. 基本用法

当程序出现错误时,可使用 cerr 输出错误信息。

#include <iostream>
using namespace std;int main() {ifstream file("nonexistent.txt");if (!file.is_open()) {cerr << "Error: Could not open file!" << endl;return 1;}// 其他操作return 0;
}
2. 无缓冲特性

cerr 的输出不会被缓冲,这在需要立即显示错误信息的场景下非常重要。

// 模拟一个长时间运行的进程
for (int i = 0; i < 1000000; ++i) {if (i % 100000 == 0) {cerr << "Processing iteration " << i << endl;  // 立即显示}// 处理逻辑
}
3. 重定向错误输出

cout 一样,cerr 的输出也能被重定向。

ofstream errorFile("errors.log");
streambuf* oldBuf = cerr.rdbuf();
cerr.rdbuf(errorFile.rdbuf());cerr << "This error will be logged to errors.log" << endl;cerr.rdbuf(oldBuf);  // 恢复

三、clog:标准日志流

clog 也是 std::ostream 类的对象,用于输出日志信息。它和 cerr 的区别在于,clog 的输出是经过缓冲的。

1. 基本用法

clog 适用于记录程序的执行状态等日志信息。

#include <iostream>
using namespace std;void log(const string& message) {clog << "[LOG] " << message << endl;
}int main() {log("Starting application...");// 程序逻辑log("Application terminated successfully.");return 0;
}
2. 缓冲特性

clog 的输出会先被存储在缓冲区中,直到缓冲区满或者遇到刷新操作。

clog << "This is a log message.";  // 可能不会立即显示
clog << flush;  // 手动刷新缓冲区
3. 日志重定向

同样可以对 clog 的输出进行重定向。

ofstream logFile("app.log");
clog.rdbuf(logFile.rdbuf());clog << "Logging to file..." << endl;  // 写入文件

四、三者的对比与选择

特性coutcerrclog
缓冲机制有缓冲无缓冲有缓冲
默认输出目标标准输出标准错误标准错误
典型应用场景普通程序输出错误信息日志记录
是否可重定向
选择建议:
  • 当需要输出程序的正常结果时,应使用 cout
  • 遇到错误情况,需要立即显示错误信息时,要使用 cerr
  • 进行程序调试或者记录执行状态等日志操作时,适合使用 clog

五、高级应用场景

1. 自定义流缓冲区

可以通过继承 streambuf 类来创建自定义的流缓冲区。

class MyBuffer : public streambuf {
protected:int overflow(int c) override { //override C++11 特性,显式声明该函数重写基类的虚函数,提高代码安全性if (c != traits_type::eof()) { //获取流特性中定义的 EOF(文件结束符)值// 自定义处理逻辑cout << "*" << static_cast<char>(c) << "*";}return traits_type::not_eof(c);}
};// 使用自定义缓冲区
MyBuffer buf;
ostream customOut(&buf);
customOut << "Test" << endl;  // 输出:*T*e*s*t*
2. 多线程环境下的输出

在多线程环境中使用输出流时,需要进行同步操作,以避免输出混乱。

#include <iostream>
#include <mutex>
#include <thread>
using namespace std;mutex coutMutex;void worker(int id) {lock_guard<mutex> lock(coutMutex);cout << "Thread " << id << " is working." << endl;
}int main() {thread t1(worker, 1);thread t2(worker, 2);t1.join();t2.join();return 0;
}
3. 结合 RAII 管理流重定向

利用 RAII(资源获取即初始化)技术,可以更安全地管理流重定向。

class StreamRedirect {
public:StreamRedirect(ostream& stream, streambuf* newBuf): stream(stream), oldBuf(stream.rdbuf()) {stream.rdbuf(newBuf);}~StreamRedirect() {stream.rdbuf(oldBuf);}private:ostream& stream;streambuf* oldBuf;
};// 使用示例
ofstream file("output.txt");
{StreamRedirect redirect(cout, file.rdbuf());cout << "Redirected output" << endl;  // 写入文件
}  // 离开作用域时自动恢复

六、注意事项

  1. 性能考量

    • 无缓冲的输出(如 cerr)会带来一定的性能开销,所以在性能敏感的场景中应当谨慎使用。
    • 有缓冲的输出(如 coutclog)在频繁刷新缓冲区时,也可能会影响性能。
  2. 线程安全

    • 标准输出流本身并不是线程安全的,在多线程环境下使用时需要进行同步处理。
  3. 资源管理

    • 重定向流缓冲区后,要确保在适当的时候恢复原来的缓冲区。

七、总结

  • cout:是最常用的输出流,适用于普通的程序输出,输出内容会被缓冲。
  • cerr:主要用于输出错误信息,输出不会被缓冲,能保证错误信息立即显示。
  • clog:适用于记录日志,输出会被缓冲,有助于提高性能。
http://www.dtcms.com/a/286217.html

相关文章:

  • 深度解析Linux文件I/O三级缓冲体系:用户缓冲区→标准I/O→内核页缓存
  • RV126平台NFS网络启动终极复盘报告
  • 深入解析 vLLM 分布式推理与部署策略
  • ChatBot 还能记住我是谁?深度解析对话系统中的记忆机制
  • 一文通晓25G多模光模块:SFP-25G-SR和SFP-25G-ESR的区别
  • 机器学习基础:线性回归算法详解(原理+代码+实战)
  • 视频讲解:XGBoost梯度提升树原理及用Python对房价等数据集多案例应用分析
  • 网络(TCP)
  • 百炼MCP与IoT实战(三):手搓自定义MCP Server与阿里云FC配置
  • CMU-15445(2024fall)——PROJECT#0
  • iOS 文件深度调试实战 查看用户文件 App 沙盒 系统文件与日志全指南
  • 【橘子分布式】gRPC(编程篇-上)
  • vue2 面试题及详细答案150道(91 - 100)
  • 人工智能概念之九:深度学习概述
  • kafka 单机部署指南(KRaft 版本)
  • 基于MATLAB的GA遗传算法优化BP神经网络的数据分类预测方法应用
  • 在ComfyUI中CLIP Text Encode (Prompt)和CLIPTextEncodeFlux的区别
  • docker gitlab 备份 恢复 版本升级(16.1.1到18.2.0)
  • SpringAI核心特性与Prompt工程
  • 六个直播回放的功能,助力在线教育蓬勃发展!
  • AR技术重塑电力巡检:效率提升与智能升级
  • Kubernetes (k8s)、Rancher 和 Podman 的异同点分析
  • 题解:CF1617C Paprika and Permutation
  • Redis学习其二(事务,SpringBoot整合,持久化RDB和AOF)
  • MySQL查询优化与事务实战指南
  • 【PTA数据结构 | C语言版】我爱背单词
  • 前端埋坑之element Ui 组件el-progress display:flex后不显示进度条解决方案
  • Java实战:实时聊天应用开发(附GitHub链接)
  • http性能测试命令ab
  • IntelliJ IDEA大括号格式设置:换行改行尾