2018.10.18 10.2初识泛型算法--10.3定制操作(昨天的博客没了)

10.9
答案

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

inline void output_words(vector<string> &v)
{
	for(auto iter = v.begin(); iter != v.end(); ++iter)
		cout << *iter << " " ;
	cout << endl;
}

void elimdups(vector<string>  &words)
{
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开文件" << endl;
		exit(1);
	}
	vector<string> v1;
	string s;
	while (in>>s)
		v1.push_back(s);
	elimdups(words);
	return 0;
}

10.10
算法不会执行容器操作
答案的
泛型算法的一大优点就是“泛型”,一个算法可用于多种不同的数据类型,算法与所操作的数据结构分离。这对编程效率的提高是巨大的。
要做到分离,重要的技术手段就是使用迭代器作为俩者的桥梁。算法不操作具体的容器,从而也不会和特定的容器绑定,算法只操作迭代器,有迭代器真正实现对容器的访问。算法根本不知道容器的存在。是否要改变容器的大小时迭代器的责任

10.11

#include <isotream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

inline void output_words(const string &s)
{
	for(auto iter = s.begin(); iter != s.end(); ++iter)
		cout << *iter <<" " ;
	cout << endl;
}

void elimdups(vector<string> &words)
{
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
	stable_sort(words.begin(), words.end());
}

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开文件" << endl;
		exit(1);
	}
	string word;
	vector<string> v;
	while (in >> word)
	{
		v.push_back(word);
	}
	elimdups(v);
	for (const auto &i : s)
		cout << i << " " ;
	cout << endl;
	return 0;
}

10.12
答案

inline bool compareisbn(const Sales_data &lhs, const Sales_data rhs)
{
	return lhs.isbn() < rhs.isbn();
}
include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algoriyhm>
#include "Sales_data.h"

using namespace std;

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开文件" << endl;
		exit(1)'
	}
	
	vector<Sales_data> sdv;
	Sales_data sd;
	while(read(in, sd))
		sdv.push_back(sd);
	
	sort(sdv.begin(), sdv.end(), compareisbn);
	for (const auto &i : sdv )
	{
		print(cout , s);
		cout << endl;
	}
	return 0
}

10.13

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

inline void input(vector<string> &s)
{
	string ss;
	while (cin>>ss)
		s.push_back(ss);
}

inline void output(vector<string> &s)
{
	for(auto iter = s.begin(); iter != s.end() ; ++iter)
		cout << *iter << " " ;
	cout << endl;
}

bool check(const string &s)
{
	bool b = false;
	if (s.size() >= 5)
		b = true;
	return b;
}

int main(int argc, char *argv[])
{
	vector<string> s;
	input(s);
	/*ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开文件" << endl;
		exit(1);
	}*/
	
	string s;
	vector<string> vs;
	while(in >>s)
		vs.push_back(s);
	auto end_p = partition(vs.begin(), vs.end(), check);
	vs = vs.erase(end_p, vs.end);
	output(vs);
	return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

inline void input(vector<string> &s)
{
	string ss;
	while (cin>>ss)
		s.push_back(ss);
}

inline void output(vector<string> &s)
{
	for(auto iter = s.begin(); iter != s.end() ; ++iter)
		cout << *iter << " " ;
	cout << endl;
}

bool check(const string &s)
{
	bool b = false;
	if (s.size() >= 5)
		b = true;
	return b;
}

int main(int argc, char *argv[])
{
	vector<string> vs;
	input(vs);
	output(vs);
	/*ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开文件" << endl;
		exit(1);
	}
	
	string s;
	vector<string> vs;
	while(in >>s)
		vs.push_back(s);*/
	auto end_p = partition(vs.begin(), vs.end(), check);
//	auto iter1 = vs.end();
	vs.erase(end_p, vs.end());
	output(vs);
	return 0;
}

答案的

跟一类似

check()

bool check(const string &s1)
{
	return s1.size() >= 5;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值