C++ Primer 课后练习10.16,10.17,10.18,10.19

//课后练习10.16
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
using namespace std;
void elimdups(vector<string> & words)
{
//按字典序排序
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
void biggies(vector<string> & words,
vector<string>::size_type sz)
{
elimdups(words); //将word按字典序排序,删除重复单词
stable_sort(words.begin(), words.end(),
[](const string& s1, const string & s2){return s1.size() < s2.size(); });
auto wc = find_if(words.begin(), words.end(),
[sz](const string s){return s.size() >= sz; });
auto count = words.end() - wc;
for_each(wc, words.end(),
[](const string & s){cout << s << endl; });
}
int main(void)
{
vector<string> words;
string word;
while (cin >> word)
{
words.push_back(word);
}
biggies(words, 5);
return 0;
}

**********************************************************************
//课后练习10.17
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<string>  
#include<numeric>  
#include<fstream>  
using namespace std;
class Sales_data
{
public:
	Sales_data(string ss) :bookNo(ss){}
	string isbn(){
		return bookNo;
	}
private:
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0;
};
bool compareIsbn(Sales_data & s1, Sales_data & s2)
{
	return s1.isbn().size() < s2.isbn().size();
}//注意const不能使用非const数据成员  
int main(void)
{
	ifstream is("file1.txt");
	vector<string> vec;
	string word;
	while (is >> word)
	{
		vec.push_back(word);
	}
	Sales_data s1(vec[0]), s2(vec[1]), s3(vec[2]), s4(vec[3]), s5(vec[4]);
	vector<Sales_data> vec1{ s1, s2, s3, s4, s5 };
	sort(vec1.begin(), vec1.end(), 
		[](Sales_data &s1, Sales_data & s2){return s1.isbn().size() < s2.isbn().size(); });
	for (auto & ss : vec1)
	{
		cout << ss.isbn() << " ";
	}




}
/********************************************************************/
课后练习10.18
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
using namespace std;
void elimdups(vector<string> & words)
{
	//按字典序排序
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}
void biggies(vector<string> & words,
	vector<string>::size_type sz)
{
	elimdups(words); //将word按字典序排序,删除重复单词
	//stable_sort(words.begin(), words.end(),
	//	[](const string& s1, const string & s2){return s1.size() < s2.size(); });
	auto wc = partition(words.begin(), words.end(),
		[sz](const string s){return s.size() < sz; });
	auto count = words.end() - wc;
	for_each(wc,words.end(),
		[](const string & s){cout << s << endl; });
}
int main(void)
{
	vector<string> words{ "the", "quick", "red", "fox", "jumps",
		"over", "the", "slow", "red", "turtle" };
	biggies(words, 4);
	return 0;
}

/*******************************************************************/
//练习10.19
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
using namespace std;
void elimdups(vector<string> & words)
{
	//按字典序排序
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}
void biggies(vector<string> & words,
	vector<string>::size_type sz)
{
	elimdups(words); //将word按字典序排序,删除重复单词
	//stable_sort(words.begin(), words.end(),//按大小排序,相同的保存字母顺序
	//	[](const string& s1, const string & s2){return s1.size() < s2.size(); });
	auto wc = stable_partition(words.begin(), words.end(),
		[sz](const string s){return s.size() < sz; });
	auto count = words.end() - wc;
	for_each(wc,words.end(),
		[](const string & s){cout << s << endl; });
}
int main(void)
{
	vector<string> words{ "the", "quick", "red", "fox", "jumps",
		"over", "the", "slow", "red", "turtle" };

	biggies(words, 4);
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值