练习10.9
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
using namespace std;
void elimDups(vector<string> &words);
vector<string> str;
string story;
while (cin >> story)
{
str.push_back(story);
}
elimDups(str);
for (auto word : str)
{
cout << word << " ";
}
cout << endl;
return 0;
}
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
练习10.11
#ifndef LSORT_H
#define LSORT_H
using namespace std;
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
#endif
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include"lsort.h"
int main()
{
using namespace std;
void elimDups(vector<string> &words);
bool isShorter(const string &s1, const string &s2);
vector<string> str;
string story;
while (cin >> story)
{
str.push_back(story);
}
elimDups(str);
stable_sort(str.begin(), str.end(), isShorter);//按长度重新排序,长度相同的单词维持字典序;
for (const auto&s : str)
{
cout << s << " ";
}
cout << endl;
return 0;
}
练习10.12
#ifndef LSORT_H
#define LSORT_H
using namespace std;
class Sales_data
{
private:
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
public:
string isbn() const { return bookNo; }
Sales_data() = default;
Sales_data(const string &s):bookNo(s){}
Sales_data(const string &s,unsigned n,double p):bookNo(s),unit_sold(n),revenue(p*n){}
};
bool comparaIsbn(const Sales_data &s1, const Sales_data &s2)
{
return s1.isbn().size() < s2.isbn().size();
}
#endif
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include"lsort.h"
int main()
{
using namespace std;
Sales_data s1("shiahng",3,2), s2("wudi",6,7), s3("shisahng",7,8), s4("sunxiyang",6,5), s5("xiyangyang",1,2);//必须要有对应的构造函数
vector<Sales_data> vec1{ s1, s2, s3, s4, s5 };
sort(vec1.begin(), vec1.end(), comparaIsbn);
for (auto & ss : vec1)
{
cout << ss.isbn() << " ";
}
cout << endl;
return 0;
}
练习10.13
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
bool compareSize(const string &word)
{
return word.size() <= 5;
}
int main()
{
vector<string> vec;
string s;
while (cin >> s)
vec.push_back(s);
auto end_partition = partition(vec.begin(), vec.end(), compareSize);
for (auto ss : vec)
cout << ss << " ";
cout << endl;
return 0;
}
习题10.18
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
void biggies(vector<string> &words, vector<string>::size_type sz)
{
auto end_partition = partition(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz; });//返回为第一个不为ture的迭代器
auto count = end_partition - words.begin();
cout << count << endl;
for_each(words.begin(), end_partition, [](const string &s) {cout << s << " "; });
cout << endl;
}
int main()
{
vector<string> vec = { "shi","shang","bryant","xunxiyang","xiyangyang","kobe","jamesss","harden" };
biggies(vec,5);
}
练习10.22
使用find_if
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &str, string::size_type sz)
{
return str.size() <= sz;
}
void elimdups(vector<string> & words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
bool ishorter(const string &s1, const string &s2)//从长到短排序
{
return s1.size() > s2.size();
}
ostream & print(ostream &os, const string & s, char c)
{
return os << s << c;
}
void biggies(vector<string> & words,
vector<string>::size_type sz)
{
elimdups(words);
stable_sort(words.begin(), words.end(), bind(ishorter, _1, _2));//排序完为{ "sunxiyang","shisahng","shang","wudi","shi", }
//auto checksz = bind(check_size, _1, sz);
auto wc1 = find_if(words.begin(), words.end(), [sz](const string &ss) {return ss.size() <= sz; });//此时wc1迭代器指向"shang"
auto count = words.end() - wc1;
cout << count << endl;
for_each(words.begin(), words.end(), bind(print, ref(cout), _1, ' '));
}
int main()
{
vector<string> vec = { "shisahng","shi","shang","wudi","sunxiyang" };
biggies(vec, 6);
return 0;
}
使用count_if:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &str, string::size_type sz)
{
return str.size() <= sz;
}
int main()
{
vector<string> vec = { "shisahng","shi","shang","wudi","sunxiyang" };
auto wc = count_if(vec.begin(), vec.end(), bind(check_size, _1, 5));
cout << wc << endl;
return 0;
}
练习10.24
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const int &sz, const int &i)
{
return sz > i;
}
ostream &print(ostream &os, int &s)
{
return os << endl << s ;
}
int main()
{
vector<int> vec = { 2,4,8,10};
string word;
while (cin >> word)
{
auto wc = find_if(vec.begin(), vec.end(), bind(check_size, _1, word.size()));//调用check_size(vec元素,word.size())
print(cout, *wc);
}
return 0;
}