c – 是否可以同时在两个对象上使用插入 *** 作符?

c – 是否可以同时在两个对象上使用插入 *** 作符?,第1张

概述例如,如果我想在两个对象上使用提取运算符将相同的数据发送到两个对象以获取语法快捷方式 (out_file, cout) << "\n\nTotal tokens found: " << statistics[0] << "\n\nAlphaNumeric Tokens: " << statistics[1] << "\n\nPunctuation characte 例如,如果我想在两个对象上使用提取运算符将相同的数据发送到两个对象以获取语法快捷方式

(out_file,cout) << "\n\nTotal tokens found: " << statistics[0] << "\n\nAlphaNumeric Tokens: " << statistics[1]                << "\n\nPunctuation character found: " << statistics[2] << "\nNumber of whitespace: " << statistics[3]                << "\nNumber of newlines: " << statistics[4] << "\n\nTokens are stored in out file\n\nPress Enter to exit....";

那么数据是否同时应用于out_file和cout?
out_file是fstream ..

解决方法 您可以使用 boost::iostreams::tee_device将数据发送到一对流.

teeing.cpp

#include <boost/iostreams/stream.hpp>#include <boost/iostreams/tee.hpp>#include <fstream>#include <iostream>int main(){    typedef boost::iostreams::tee_device<std::ostream,std::ostream> Tee;    typedef boost::iostreams::stream<Tee> TeeStream;    std::ofstream out_file("./out_file.log");    Tee tee(std::cout,out_file);    TeeStream both(tee);    both << "This is a test!" << std::endl;}

建立:

> clang++ -I/path/to/boost/1.54.0/include teeing.cpp -o teeing

跑:

> ./teeingThis is a test!

校验:

> cat ./out_file.log This is a test!
总结

以上是内存溢出为你收集整理的c – 是否可以同时在两个对象上使用插入 *** 作符?全部内容,希望文章能够帮你解决c – 是否可以同时在两个对象上使用插入 *** 作符?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1226977.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存