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

《Essential C++》系列笔记之第三章(泛型编程风格)之第十节(使用iostream Iterator)_第1张图片
代码实践

#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 

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 
using namespace std;
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


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()*/

	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(越界了) 干就完事了

你可能感兴趣的:(《Essential,C++》系列笔记)