13 Stream(续)

13 Stream(续)

13.10 连接input stream和output stream

1、以tie()完成松耦合

如std::cin.tie(&std::cout);可以保证在cin输入之前,一定会情况output缓冲区,此处即对cout调用flush()。如果参数为NULL,则解除连接。

 

2、以stream缓冲区完成紧耦合:使不同的stream共享同一个缓冲区

如ostream hexout(cout.rdbuf());用cout的stream缓冲区初始化hexout

注意:只有basic_istream和basic_ostream不销毁缓冲区。其他stream都会销毁它们最初分配的stream缓冲区,但是不会销毁以rdbuf()设置的缓冲区。如std::ofstream file("cout.txt"); file为局部对象,将在程序区段结束时被销毁,其对应的stream缓冲区也一并被销毁。

 

4、用于读写的stream

std::filebuf buffer; std::ostream out(&buffer); std::istream in(&buffer);

buffer.open("example.txt", std::ios::in | std::ios::out);

两个不同的stream对象in、out分别用于读取和改写,注意读写位置是共享的。

 

13.11 String stream class——<sstream>

1、型别

istringstream、ostringstream、stringstream、stringbuf。(同样存在wistringstream等)

举例:ostringstream os; os<<"oct:"<<oct<<15; cout<<os.str()<<endl;

 

13.12 自定义型别的I/O操作符

1、实作一个output操作符

#include <iostream>
#include <sstream>

template <class charT, class traits>
inline std::basic_ostream<charT, traits>&
operator << (std::basic_ostream<charT, traits>& strm, const Fraction& f) //分数的输出
{//先将f输出到字符串流s中,再从s输出到strm中,从而防止格式问题。
	std::basic_ostringstream<charT,traits> s;
	s.copyfmt(strm);
	s.width(0);  //防止对于对于16/100,输出为“16   /100”

	s<<f.numerator<<'/'<<f.denominator();
	strm<<s.str();
	return strm;
}


2、实作一个input操作符

#include <iostream>

template <class charT, class traits>
inline std::basic_ostream<charT, traits>&
operator >> (std::basic_ostream<charT, traits>& strm, const Fraction& f) //分数的输入
{
	int n, d;
	strm>>n;
	if(strm.peek() == '/'){
		strm.ignore();
		strm>>d;
	}else{d = 1;}
	if(d == 0){
		strm.setstate(std::ios::failbit);
		return strm;
	}
	if(strm)
		f = Fraction(n,d);
	return strm;
}


 13.13 stream buffer class

1、成员函数

如果发生错误,sputc()返回traits_type::eof()。

 

2、缓冲区迭代器——<iterator>

output stream缓冲区迭代器:std::ostreambuf_iterator<char> bufWriter(std::cout);string hello("hello");copy(hello.begin(), hello.end(), bufWriter);

input stream缓冲区迭代器:istreambuf_iterator

#include <iostream>
#include <iterator>

using namespace std;

int main()
{
	istreambuf_iterator<char> inpos(cin);
	istreambuf_iterator<char> endpos;
	ostreambuf_iterator<char> outpos(cout);

	while (inpos != endpos) {
		*outpos = *inpos;
		++inpos;
	//	++outpos;
	}
	
	return 0;
}


13.14 关于性能

1、cin、cout、cerro、clog等与C标准串流(stdin、stdout、stderr)的同步,可能带来某些不必要的额外负担。

2、I/O是否具备缓冲能力,很大程度上影响了效率。

3、进行无格式I/O时,最好直接使用stream缓冲区。如:cout<<cin.rdbuf();或者cin>>noskipws>>cout.rdbuf();

--2025-08-19 11:09:47-- https://github.com/iMoonLab/yolov13/releases/download/yolov13/yolov13x.pt Resolving github.com (github.com)... 20.205.243.166 Connecting to github.com (github.com)|20.205.243.166|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://release-assets.githubusercontent.com/github-production-release-asset/1006122056/834870e2-fcfc-42a0-9896-0dfc767825b6?sp=r&sv=2018-11-09&sr=b&spr=https&se=2025-08-19T12%3A10%3A02Z&rscd=attachment%3B+filename%3Dyolov13x.pt&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2025-08-19T11%3A09%3A20Z&ske=2025-08-19T12%3A10%3A02Z&sks=b&skv=2018-11-09&sig=bPQuYlbsYyTkGzjhR8TFQrXtOKAz9NrBJkxOCpOctak%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc1NTYwMjA4OCwibmJmIjoxNzU1NjAxNzg4LCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.GbRwWDzAg0X1X3fgeCwNL_a0KAKBEKPnXEB2JL_7rY8&response-content-disposition=attachment%3B%20filename%3Dyolov13x.pt&response-content-type=application%2Foctet-stream [following] --2025-08-19 11:09:48-- https://release-assets.githubusercontent.com/github-production-release-asset/1006122056/834870e2-fcfc-42a0-9896-0dfc767825b6?sp=r&sv=2018-11-09&sr=b&spr=https&se=2025-08-19T12%3A10%3A02Z&rscd=attachment%3B+filename%3Dyolov13x.pt&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2025-08-19T11%3A09%3A20Z&ske=2025-08-19T12%3A10%3A02Z&sks=b&skv=2018-11-09&sig=bPQuYlbsYyTkGzjhR8TFQrXtOKAz9NrBJkxOCpOctak%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc1NTYwMjA4OCwibmJmIjoxNzU1NjAxNzg4LCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.GbRwWDzAg0X1X3fgeCwNL_a0KAKBEKPnXEB2JL_7rY8&response-content-disposition=attachment%3B%20filename%3Dyolov13x.pt&response-content-type=application%2Foctet-stream Resolving release-assets.githubusercontent.com (release-assets.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.111.133, ... Connecting to release-assets.githubusercontent.com (release-assets.githubusercontent.com)|185.199.109.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 257795022 (246M) [application/octet-stream] Saving to: ‘yolov13x.pt’ yolov13x.pt 8%[======> ] 20.00M 14.4KB/s in 22m 7s 2025-08-19 11:31:55 (15.4 KB/s) - Connection closed at byte 20971520. Retrying. --2025-08-19 11:31:56-- (try: 2) https://release-assets.githubusercontent.com/github-production-release-asset/1006122056/834870e2-fcfc-42a0-9896-0dfc767825b6?sp=r&sv=2018-11-09&sr=b&spr=https&se=2025-08-19T12%3A10%3A02Z&rscd=attachment%3B+filename%3Dyolov13x.pt&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2025-08-19T11%3A09%3A20Z&ske=2025-08-19T12%3A10%3A02Z&sks=b&skv=2018-11-09&sig=bPQuYlbsYyTkGzjhR8TFQrXtOKAz9NrBJkxOCpOctak%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc1NTYwMjA4OCwibmJmIjoxNzU1NjAxNzg4LCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.GbRwWDzAg0X1X3fgeCwNL_a0KAKBEKPnXEB2JL_7rY8&response-content-disposition=attachment%3B%20filename%3Dyolov13x.pt&response-content-type=application%2Foctet-stream Connecting to release-assets.githubusercontent.com (release-assets.githubusercontent.com)|185.199.109.133|:443... connected. HTTP request sent, awaiting response... 618 jwt:expired 2025-08-19 11:31:57 ERROR 618: jwt:expired.
最新发布
08-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值