list与流迭代器stream_iterator

运行代码:

cpp 复制代码
//list与流迭代器
#include"std_lib_facilities.h"
//声明Item类
struct Item
{
	string name;
	int iid;
	double value;

	Item():name(" "),iid(0),value(0.0){}
	Item(string ss,int ii,double vv):name(ss),iid(ii),value(vv){}
	
	friend istream& operator>>(istream& is, Item& ii);
	friend ostream& operator<<(ostream& os, const Item& ii);

};

//------------------------------------------------------------
//重载Item类的输入和输出操作符
istream& operator>>(istream& is, Item& ii)
{
	is >> ii.name >> ii.iid >> ii.value;
	return is;
}

ostream& operator<<(ostream& os, const Item& ii)
{
	os << ii.name << " " << ii.iid << " " << ii.value;
	return os;
}

//----------------------------------------------------------

int main()
try
{
	cout << "输入读取文件名: ";
	string from_file;
	cin >> from_file;

	ifstream is(from_file.c_str());
	if (!is)error("can't open ", from_file);

	istream_iterator<Item>ii(is);
	istream_iterator<Item>eos;
	ostream_iterator<Item>oo(cout, "\n");

	list<Item>ll(ii, eos);
	copy(ll.begin(), ll.end(), oo);

	return 0;
}
catch (exception& e) {
	cerr << "error:" << e.what() << '\n';
	keep_window_open();
	return 1;
}
catch (...) {
	cerr << "Oops:unknown exception!\n";
	keep_window_open();
	return 2;
}

读取文件:Item_file.txt

Max 2 43.2
Jane 3 34.2
Tom 3 32.4
Mary 8 23.0
Peter 5 32.5
Max 2 43.2
Jane 3 34.2
Tom 3 32.4
Mary 8 23.0
Peter 5 32.5

运行结果:

相关推荐
m0_639906943 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
Sakura_j_h_3 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
ZhengEnCi4 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
程序喵大人4 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
秋天的怀念_David4 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi5 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6185 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang5 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
jimy16 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
2601_970703837 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法