10.14
auto sum = [](int a,int b){ return a + b }
10.15
auto sum2 = [int c](int a){ return a + c }
10.16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void println(const std::vector<std::string> & vs) {
for (auto a : vs) {
std::cout << a << " ";
}
std::cout << std::endl;
}
void elimDups(std::vector<std::string> &vs)
{
std::sort(vs.begin(), vs.end());
println(vs);
auto new_end = std::unique(vs.begin(), vs.end());
println(vs);
vs.erase(new_end, vs.end());
}
void biggies(std::vector<std::string> &vs, std::size_t sz)
{
using std::string;
elimDups(vs);
std::stable_sort(vs.begin(), vs.end(), [](string const& lhs, string const& rhs) {
return lhs.size() < rhs.size();
});
println(vs);
auto wc = std::find_if(vs.begin(), vs.end(), [sz](string const& s) {
return s.size() >= sz;
});
println(vs);
std::for_each(wc, vs.end(), [](const string &s) {
std::cout << s << " ";
});
std::cout << std::endl;
}
int main()
{
std::vector<std::string> v
{
"1234","1234","1234","hi~", "alan", "alan", "cp"
};
std::cout << "ex10.16: ";
biggies(v, 3);
std::cout << std::endl;
return 0;
}
10.17
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "Sales_data.h"
int main()
{
Sales_data d1("aa"), d2("aaaa"), d3("aaa"), d4("z"), d5("aaaaz");
std::vector<Sales_data> v{ d1, d2, d3, d4, d5 };
std::sort(v.begin(), v.end(), [](const Sales_data &sd1, const Sales_data &sd2){
return sd1.isbn().size() < sd2.isbn().size();
});
for(const auto &element : v)
std::cout << element.isbn() << " ";
std::cout << std::endl;
return 0;
}
10.18
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void println(const std::vector<std::string> & vs) {
for (auto a : vs) {
std::cout << a << " ";
}
std::cout << std::endl;
}
void elimDups(std::vector<std::string> &vs)
{
std::sort(vs.begin(), vs.end());
println(vs);
auto new_end = std::unique(vs.begin(), vs.end());
println(vs);
vs.erase(new_end, vs.end());
}
void biggies_partition(std::vector<std::string> &vs, std::size_t sz)
{
using std::string;
elimDups(vs);
auto pivot = partition(vs.begin(), vs.end(), [sz](const std::string &s){
return s.size() >= sz;}
);
for(auto it = vs.cbegin(); it != pivot; ++it)
std::cout << *it << " ";
std::cout << std::endl;
}
int main(){
std::vector<std::string> v{
"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"
};
biggies_partition(v1, 4);
std::cout << std::endl;
return 0;
}
10.19
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void println(const std::vector<std::string> & vs) {
for (auto a : vs) {
std::cout << a << " ";
}
std::cout << std::endl;
}
void elimDups(std::vector<std::string> &vs)
{
std::sort(vs.begin(), vs.end());
println(vs);
auto new_end = std::unique(vs.begin(), vs.end());
println(vs);
vs.erase(new_end, vs.end());
}
void biggies_partition(std::vector<std::string> &vs, std::size_t sz)
{
using std::string;
elimDups(vs);
auto pivot = stable_partition(vs.begin(), vs.end(), [sz](const std::string& s){
return s.size() >= sz;
});
for(auto it = vs.cbegin(); it != pivot; ++it)
std::cout << *it << " ";
std::cout << std::endl;
}
int main(){
std::vector<std::string> v{
"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"
};
biggies_stable_partition(v2, 4);
std::cout << std::endl;
return 0;
}