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

运行结果:

相关推荐
仰泳的熊猫1 天前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
无极低码1 天前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发1 天前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
Thera7771 天前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
罗超驿1 天前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
superior tigre1 天前
22 括号生成
算法·深度优先
君义_noip1 天前
信息学奥赛一本通 1952:【10NOIP普及组】三国游戏 | 洛谷 P1199 [NOIP 2010 普及组] 三国游戏
c++·信息学奥赛·csp-s
努力也学不会java1 天前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
旖-旎1 天前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针