《Essential C++》系列笔记之第三章(泛型编程风格)之第十节(使用iostream Iterator)

在这里插入图片描述
代码实践

#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>

void OperatorString()
{
	string str;
	vector<string> svec;

	while (cin >> str)
	{
		svec.push_back(str);
	}

	sort(svec.begin(), svec.end());

	for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); iter++)
	{
		cout << *iter << ' ';
	}
	cout << endl;
}

void demo_1()
{
	istream_iterator<string> is(cin);  //first iterator
	istream_iterator<string> eof;      //last iterator

	vector<string> svec;
	copy(is, eof, back_inserter(svec));

	sort(svec.begin(), svec.end());

	ostream_iterator<string> os(cout, " "); //第二个参数用来表示分隔符,也可以没有 " "
	copy(svec.begin(), svec.end(), os);
}

void demo_2()
{
	ifstream infile("infile.txt");
	ofstream outfile("outfile.txt");

	if ( !infile || !outfile )
	{
		cerr << "Error: !infile || !outfile" << endl;
		return;
	}

	istream_iterator<string> first(infile);
	istream_iterator<string> eof;

	vector<string> words;
	copy(first, eof, back_inserter(words));

	sort(words.begin(), words.end());

	ostream_iterator<string> os(outfile, " ");
	copy(words.begin(), words.end(), os);
}

int main()
{
	//demo_0
	//OperatorString();

	//demo_1
	//demo_1();

	//demo_2
	demo_2();
	

	system("pause");
	return 0;
}

习题练习

#include <iostream>
using namespace std;
#include <set>
#include <map>
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <functional>


void demo_3_1()
{
	ifstream infile("infile.txt");
	ofstream outfile("outfile.txt");

	if (!infile || !outfile)
	{
		cerr << "!infile || !outfile" << endl;
		return;
	}

	map<string, int> twords;

	string str[] = { "a","an","or","the","and","but" };
	vector<string> svec(str, str + 6);
	set<string> filter;
	filter.insert(svec.begin(), svec.end());

	string words;
	while (infile >> words)
	{
		if (filter.count(words))
		{
			continue;
		}
		else
		{
			twords[words]++;
		}
	}

	cout << "You can find words" << endl;
	string find;
	while (cin >> find)
	{
		if (twords.count(find))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
	}

	map<string, int>::iterator iter = twords.begin();
	for (; iter != twords.end(); iter++)
	{
		outfile << iter->first << ' ' << iter->second << endl;
	}

}


inline bool str_lenth(string str1, string str2)
{
	return (str1.length() < str2.length() ? true : false);
}
void demo_3_2()
{
	ifstream infile("infile.txt");
	if (!infile)
	{
		cerr << "!infile" << endl;
		return;
	}

	vector<string> svec;

	string words;
	while (infile >> words)
	{
		svec.push_back(words);
	}

	sort(svec.begin(), svec.end(), str_lenth); ///*, greater<string>()*/

	vector<string>::iterator iter = svec.begin();
	for (; iter != svec.end(); iter++)
	{
		cout << *iter << " ";
	}
	cout << endl;

}

void demo_3_3()
{
	map<string, string> family;

}

void demo_3_4()
{
	ofstream odd_file("odd_file.txt");
	ofstream even_file("even_file.txt");

	if (!odd_file || !even_file)
	{
		cerr << "!odd_file || !even_file" << endl;
		return;
	}

	vector<int> ivec;
	istream_iterator<int> first(cin);
	istream_iterator<int> last;
	copy(first, last, back_inserter(ivec));

	ostream_iterator<int> odd(odd_file, " ");
	ostream_iterator<int> even(even_file, "\n");

	vector<int>::iterator iter = ivec.begin();
	for (; iter != ivec.end(); iter++)
	{
		if ((*iter) % 2)
		{
			//odd
			copy(iter, iter + 1, odd);
		}
		else
		{
			//even
			copy(iter, iter + 1, even);
		}
	}

}

int main()
{
	//demo_3_1();

	//demo_3_2();

	//demo_3_3();

	//demo_3_4();

	system("pause");
	return 0;
}

今天还是20200313又是14(越界了) 干就完事了👌

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值