11.3

本章大意
详细分析了各类迭代器。

细节摘录
1. 一些别的类型的迭代器可以当作“容器”或者“函数”看待
2.

课后习题
1. 差别仅在于插入元素位置的不同
2. 不明白题意
3.
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
	list<string> l;
	string s;

	while (cin >> s) {
		l.push_back(s);
	}

	cout << "录入结果" << endl;
	for (list<string>::iterator it = l.begin(); it!=l.end(); it++) {
		cout << *it << " ";
	}
	cout << endl << "....处理中.." << endl;
	
	//sort(l.begin(), l.end()); // 问题出在这句,sort算法不能用在list容器之上面? 那么有什么办法呢?(除了复制到Vector再调用sort这种方法)
	l.sort();
	vector<string> v;	
	unique_copy(l.begin(), l.end(), inserter(v, v.begin()));  // 使用之前应当进行排序,因为只能删除相邻的元素。
	cout << "处理完毕" << endl;
	for (vector<string>::iterator it = v.begin(); it!=v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	return 0;
}

4.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
	ifstream in;
	string filename;
	cout << "请输入要打开的文件名" << endl;
	cin >> filename;	
	in.open(filename.c_str());
	if (!in) {
		cout << "打开文件失败" << endl;
	}
	else
		cout << "文件打开成功" << endl;
	istream_iterator<int> cin_it(in);
	istream_iterator<int> end;
	ostream_iterator<int> out(cout, " ");
	copy(cin_it, end, out);

	cout << endl;

	return 0;
}

习题较多 暂留
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值