C++ Primer Chapter 11 generic algorithm

#if 0
//chapter11 Exercise 11.1
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>

using namespace std;

int main ()
{
	vector<int> ivec;
	int ival;
	cout << "Enter some integers(Ctrl + z to end): " << endl;
	while (cin >> ival)
	{
		ivec.push_back(ival);
	}
	cin.clear();
	int searchValue;
	cout << "Enter the value you want to search" << endl;
	cin >> searchValue;
	vector<int>::const_iterator res = find(ivec.begin(), ivec.end(), searchValue);
	cout << "the value " << searchValue 
		   << (res == ivec.end() ? "is not present" : " is present") << endl;
	system("pause");
	cout << count(ivec.begin(), ivec.end(), searchValue) 
		   << " elements in the vector have value " << searchValue << endl;
	cout << *res << endl;
	return 0;
}
#endif

#if 0
//chapter11 Exercise11.3
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
using namespace std;
int main()
{
	int ival;
	vector<int> ivec;
	cout << "Enter some integers(Ctrl + z to End)";
	while (cin >> ival)
		ivec.push_back(ival);
	cout << "the sum of integers in the vector is " 
			<< accumulate(ivec.begin(), ivec.end(), 0)
			<< endl;
	system("pause");
}
#endif

#if 0
//chapter11 Exercise11.4
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
using namespace std;
int main()
{
	double dval;
	vector<double> dvec;
	cout << "Enter some integers(Ctrl + z to End)";
	while (cin >> dval)
		dvec.push_back(dval);
	cout << "the sum of integers in the vector is " 
		<< accumulate(dvec.begin(), dvec.end(), 0)
		//这里将输出dvec中的double类型转换为int型了,再求和,所以最终得到的是各个元素整数的和
		<< endl;
	system("pause");
}
#endif

#if 0
//chapter11 Exercise11.6
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

int main()
{
	vector<int> ivec;
	fill_n(back_inserter(ivec), 10, 10);
	cout << accumulate(ivec.begin(),  ivec.end(), 0)
			<< endl;
	system("pause");
}
#endif

#if 0
//chapter11 Exercise11.9
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
using namespace std;

bool isShorter(const string &s1, const string &s2)
{
	return s1.size() < s2.size();
}

bool GT4(const string &s)
{
	return s.size() <= 4;
}

string make_plural(size_t ctr, const string &word, const string &ending)
{
	return (ctr == 1) ? word : word + ending;
}

int main()
{
	string next_word;
	vector<string> words;
	cout << "Enter some strings(Ctrl + z to end) " << endl;
	while (cin >> next_word)
	{
		words.push_back(next_word);
	}
	sort(words.begin(), words.end());
	vector<string>::iterator end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());//erase是容器自带的函数
	stable_sort(words.begin(), words.end(), isShorter);//用于相同长度的word按字典排序
	vector<string>::size_type wc = count_if(words.begin(), words.end(), GT4);
	cout << wc << " " << make_plural(wc, "word", "s")//make_plural word数大于一是在word后加s,为words为word的复数
			<< " 4 characters or longer" << endl;
	system("pause");
	return 0;
}
#endif	

#if 0
//chapter 11 Exercise11.14 15
#include <iostream>
#include <cstdlib>
#include <list>
#include <vector>
#include <algorithm>
#include <numeric>
#include <deque>
using namespace std;

int main()
{
	int ia[] = {1, 5, 4, 5, 5, 6, 5};
	list<int> ilist(ia, ia + 7);
	vector<int> ivec;
	deque<int> ideq;
#if 0
	replace_copy(ilist.begin(), ilist.end(), inserter(ivec, ivec.begin()), 3, 10);
	//replace_copy(ilist.begin(), ilist.end(), back_inserter(ivec), 3, 10);
	//replace_copy(ilist.begin(), ilist.end(), front_inserter(ivec), 3, 10);//error,因为vector不支持push_front操作
	for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it)
	{
		cout << *it << endl;
	}
#endif
	
#if 0
	replace_copy(ilist.begin(), ilist.end(), front_inserter(ideq), 3, 10);//deque支持push_front故而支持front_inserter
	for (deque<int>::iterator it = ideq.begin(); it != ideq.end(); ++it)
	{
		cout << *it << endl;
	}//output: 7 6 5 4 10 2 1
#endif
#if 0
//Exercise 11.15
	vector<int> ivec2;
	unique_copy(ilist.begin(), ilist.end(), back_inserter(ivec2));//只是作用于相邻重复元素。故而为达到作用整个vector重复
																							//重复元素的目的,要先用sort函数进行排序
	for (vector<int>::iterator it = ivec2.begin(); it != ivec2.end(); ++it)
	{
		cout << *it << endl;
	}
#endif
	system("pause");
	return 0;
}
#endif

#if 0
//chapter 11 Exercise 11.16
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iterator>

using namespace std;

int main()
{
	ifstream infile("F:/openmp/test/in.txt");//记住这边的文件名斜杠
	if (!infile)
	{
		cout << "Can not open file" << endl;
	}
	//infile.open("F:\openmp\test\in.txt");//文件名斜杠符号错误
	ostream_iterator<string> out_iter(cout, " ");
	istream_iterator<string> in_iter(infile), eof;
	copy(in_iter, eof, out_iter);
	system("pause");
	infile.close();
	return 0;
}
#endif
#if 0
//chapter 11 Exercise 11.18
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iterator>
#include <direct.h>

using namespace std;

int main()
{
	//ofstream outfileodd("F:/openmp/test/exercise11_18/odd.txt");//error. 其中exercise11_18文件夹本来没有,这将会导致错误
	if(_mkdir("F:/openmp/test/exercise11_18"))//创建文件夹exercise11_18, 头文件direct.h
	{
		cerr << "Can not make director!" << endl;
		return EXIT_FAILURE;
	}
	ofstream outfileodd("F:\\openmp\\test\\odd.txt");
	ofstream outfileeven("F:/openmp/test/even.txt");//以上两种文件名的表示方法都可以
	if (!outfileodd || !outfileeven)
	{
		cerr << "Can not open output file! " << endl;
		return EXIT_FAILURE;
	}
	ostream_iterator<int> out_iterodd(outfileodd, " ");
	ostream_iterator<int> out_itereven(outfileeven, "\n");
    istream_iterator<int> in_iter(cin), eof;
	while (in_iter != eof)
	{
		if (*in_iter % 2 == 0)
		{
			*out_itereven++ = *in_iter++;
		}
		else
		{
			*out_iterodd++ = *in_iter++;
		}
	}
	outfileodd.close();
	outfileeven.close();
	return 0;	
}
#endif

#if 0
//chapter 11 Exercise 11.19
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int ia[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
	vector<int> ivec(ia, ia + 10);
	for (vector<int>::reverse_iterator riter = ivec.rbegin(); riter != ivec.rend(); ++riter)
	{
		cout << *riter << endl;
	}
	vector<int>::reverse_iterator rcomma = find(ivec.rbegin(), ivec.rend(), 3);
	cout << *rcomma.base() << endl;
	return 0;
}

#endif

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值