//课后练习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;
}