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

运行结果:

相关推荐
⠀One0ne18 小时前
【C++ 面试题】内存对齐
c++
蓝色汪洋18 小时前
xtu oj环--唉
算法
Algo-hx18 小时前
数据结构入门 (十):“左小右大”的秩序 —— 深入二叉搜索树
数据结构·算法
Elias不吃糖18 小时前
NebulaChat 框架学习笔记:原子变量与左值引用的工程应用
c++·学习
Theliars18 小时前
Ubuntu 上使用 VSCode 调试 C++ (CMake 项目) 指南
c++·vscode·ubuntu·cmake
mjhcsp18 小时前
C++ map 容器:有序关联容器的深度解析与实战
开发语言·c++·map
将编程培养成爱好18 小时前
C++ 设计模式《账本事故:当备份被删光那天》
开发语言·c++·设计模式·备忘录模式
努力学算法的蒟蒻18 小时前
day11(11.11)——leetcode面试经典150
算法·leetcode·面试
im_AMBER19 小时前
Leetcode 51
笔记·学习·算法·leetcode·深度优先
做怪小疯子19 小时前
LeetCode 热题 100——哈希——字母异位词分组
算法·leetcode·哈希算法