#if 0 //fo/compose3.cpp p490 2021年04月06日 星期二 19时03分50秒
#include <iostream>
#include <algorithm>
#include <functional>
#include <locale>
#include <string>
char myToupper(char c){
std::locale loc;
return std::use_facet<std::ctype<char> >(loc).toupper(c);
}
int main(){
std::string s("Internationalization");
std::string sub("Nation");
std::string::iterator pos;
pos = search(s.begin(), s.end(), sub.begin(), sub.end(),
std::bind(std::equal_to<char>(),
std::bind(myToupper, std::placeholders::_1),
std::bind(myToupper, std::placeholders::_2)));
if(pos != s.end()){
std::cout << "\"" << sub << "\" is part of \"" << s << "\"" << std::endl;
}
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
"Nation" is part of "Internationalization"
#endif
#if 0 // fo/bind2.cpp p 491 2021年04月06日 星期二 19时11分26秒
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
class Person{
private:
std::string name;
public:
Person(const std::string& n): name(n){
}
void print() const{
std::cout << name << std::endl;
}
void print2(const std::string& prefix) const{
std::cout << prefix << name << std::endl;
}
};
int main(){
std::vector<Person> coll {Person("Tick"), Person("Trick"), Person("Track")};
std::for_each(coll.begin(), coll.end(),
std::bind(&Person::print, std::placeholders::_1));
std::cout << std::endl;
std::for_each(coll.begin(), coll.end(),
std::bind(&Person::print2, std::placeholders::_1, "Person: "));
std::cout << std::endl;
std::bind(&Person::print2, std::placeholders::_1, "This is: ")(Person("nico"));
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
Tick
Trick
Track
Person: Tick
Person: Trick
Person: Track
This is: nico
#endif
#if 0
#include <iostream>
int main(){
return 0;
}
#endif
#if 0//fo /fopow1.cpp p498 2021年04月06日 星期二 19时19分25秒
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
#include "fopow.hpp"
int main(){
std::vector<int> coll {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::transform(coll.begin(), coll.end(),
std::ostream_iterator<float>(std::cout, " "),
std::bind(fopow<float, int>(), 3, std::placeholders::_1));
std::cout << std::endl;
std::transform(coll.begin(), coll.end(),
std::ostream_iterator<float>(std::cout, " "),
std::bind(fopow<float, int>(), std::placeholders::_1, 3));
std::cout << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
3 9 27 81 243 729 2187 6561 19683
1 8 27 64 125 216 343 512 729
#endif
#if 0// fo /lambda1.cpp p499 2021年04月06日 星期二 19时27分58秒
#include <iostream>
int main(){
auto plus10 = [](int i){
return i+10;
};
std::cout << "+10: " << plus10(7) << std::endl;
auto plus10time2 = [](int i){
return (i+10)*2;
};
std::cout << "+10 *2: " << plus10time2(7) << std::endl;
auto pow3 = [](int i){
return i*i*i;
};
std::cout << "x*x*x: " << pow3(7) << std::endl;
auto inversDivide = [](double d1, double d2){
return d2/d1;
};
std::cout << "invdiv: " << inversDivide(49, 7) << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
+10: 17
+10 *2: 34
x*x*x: 343
invdiv: 0.142857
#endif
#if 0// fo/lambda2.cpp p500 2021年04月06日 星期二 19时34分38秒
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::vector<int> coll {1, 2, 3, 4, 5, 6, 7, 8};
long sum = 0;
std::for_each(coll.begin(), coll.end(),
[&sum](int elem){
sum += elem;
});
double mv = static_cast<double>(sum)/static_cast<double>(coll.size());
std::cout << "mean value: " << mv << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
mean value: 4.5
#endif
#if 0//fo/lambda3.cpp p502 2021年04月06日 星期二 19时39分23秒
#include <iostream>
#include <list>
#include <algorithm>
#include "print.hpp"
int main(){
std::list<int> coll {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
PRINT_ELEMENTS(coll, "coll: ");
std::list<int>::iterator pos;
int count = 0;
// pos = std::remove_if(coll.begin(), coll.end(),
// [count](int) mutable{
// return ++count == 3;
// });
//wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
// coll: 1 2 3 4 5 6 7 8 9 10
// 3rd removed: 1 2 4 5 7 8 9 10
pos = std::remove_if(coll.begin(), coll.end(),
[&count](int) {
return ++count == 3;
});
coll.erase(pos, coll.end());
PRINT_ELEMENTS(coll, "3rd removed: ");
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
coll: 1 2 3 4 5 6 7 8 9 10
3rd removed: 1 2 4 5 6 7 8 9 10
#endif
#if 0//fo / lambda4.cpp p502 2021年04月06日 星期二 19时46分56秒
#include <iostream>
#include <algorithm>
#include <locale>
#include <string>
char myToupper(char c){
std::locale loc;
return std::use_facet<std::ctype<char>>(loc).toupper(c);
}
int main(){
std::string s("Internationalization");
std::string sub("Nation");
std::string::iterator pos;
pos = search(s.begin(), s.end(), sub.begin(), sub.end(),
[](char c1, char c2){
return myToupper(c1)== myToupper(c2);
});
if(pos != s.end()){
std::cout << "\"" << sub << "\" is part of \"" << s << "\"" << std::endl;
}
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
"Nation" is part of "Internationalization"
#endif
#if 0//fo/lambda5.cpp p502 2021年04月06日 星期二 19时51分18秒
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
class Person{
private:
std::string name;
public:
Person(const std::string& n): name(n){
}
void print() const{
std::cout << name << std::endl;
}
void print2(const std::string& prefix) const{
std::cout << prefix << name << std::endl;
}
};
int main(){
std::vector<Person> coll {Person("Tick"), Person("Trick"), Person("Track")};
std::for_each(coll.begin(), coll.end(),
[](const Person& p){
p.print();
});
std::cout << std::endl;
std::for_each(coll.begin(), coll.end(),
[](const Person& p){
p.print2("Person: ");
});
std::cout << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
Tick
Trick
Track
Person: Tick
Person: Trick
Person: Track
#endif
#if 0//algo /foreach1.cpp p 519 2021年04月06日 星期二 19时59分08秒
#include "algostuff.hpp"
int main(){
std::vector<int> coll;
INSERT_ELEMENTS(coll, 1, 9);
std::for_each(coll.cbegin(), coll.cend(),
[](int elem){
std::cout << elem << ' ';
});
std::cout << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
1 2 3 4 5 6 7 8 9
#endif
#if 0//p520 //algo /foreach2.cpp 2021年04月07日 星期三 19时15分12秒
#include <iostream>
#include "algostuff.hpp"
int main(){
std::vector<int> coll;
INSERT_ELEMENTS(coll, 1, 9);
std::for_each(coll.begin(), coll.end(),
[](int& elem){
elem += 10;
});
PRINT_ELEMENTS(coll);
std::for_each(coll.begin(), coll.end(),
[=](int& elem){
elem += *coll.begin();
});
PRINT_ELEMENTS(coll);
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
11 12 13 14 15 16 17 18 19
22 23 24 25 26 27 28 29 30
#endif
#if 0//algo/foreach3.cpp p522 2021年04月07日 星期三 19时20分08秒
#include <iostream>
#include "algostuff.hpp"
class MeanValue{
private:
long num;
long sum;
public:
MeanValue(): num(0), sum(0){
}
void operator()(int elem){
num++;
sum += elem;
}
operator double(){
return static_cast<double>(sum)/static_cast<double>(num);
}
};
int main(){
std::vector<int> coll;
INSERT_ELEMENTS(coll, 1, 8);
double mv = std::for_each(coll.begin(), coll.end(),
MeanValue());
std::cout << "mean value: " << mv << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
mean value: 4.5
#endif
#if 0//algo /count1.cpp 2021年04月07日 星期四 19时24分30秒
#include "algostuff.hpp"
//#include"fuzhu.h"
bool isEven(int elem)
{
return elem%2==0;
}
bool greater_(int elem)
{
return elem>4;
}
bool greater_value(int elem,int value)
{
return elem>value;
}
int main()
{
std::vector<int> coll;
int num;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll:");
num=count(coll.begin(),coll.end(),4);//统计值为4的元素个数
std::cout<<"number of elements equal to 4: "<<num<<std::endl;
num=count_if(coll.begin(),coll.end(),isEven);//满足isEven判断式的元素个数,采用普通函数
std::cout<<"number of elements with even value: "<<num<<std::endl;
num=count_if(coll.begin(),coll.end(),std::bind2nd(std::greater<int>(),4));//大于4的元素个数,采用仿函数
std::cout<<"number of elements greater than 4: "<<num<<std::endl;
num=count_if(coll.begin(),coll.end(),greater_);//大于4的元素个数,采用普通函数
std::cout<<"number of elements greater than 4: "<<num<<std::endl;
num=count_if(coll.begin(),coll.end(),std::bind2nd(std::ptr_fun(greater_value),6));//大于value的元素个数,采用普通函数,并转换为仿函数
std::cout<<"number of elements greater than 4: "<<num<<std::endl;
system("pause");
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
coll:1 2 3 4 5 6 7 8 9
number of elements equal to 4: 1
number of elements with even value: 4
number of elements greater than 4: 5
number of elements greater than 4: 5
number of elements greater than 4: 3
sh: 1: pause: not found
#endif
#if 0 // algo/count1.cpp p524 2021年04月08日 星期四 19时25分22秒
#include "algostuff.hpp"
#include <iostream>
int main(){
std::vector<int> coll;
int num;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll:");
num=count(coll.cbegin(),coll.cend(),4);
std::cout << "number of elements equal to 4: " << num << std::endl;
num = count_if(coll.cbegin(), coll.cend(),
[](int elem){
return elem % 2 == 0;
});
std::cout << "number of elements with even value: " << num << std::endl;
num = count_if(coll.cbegin(), coll.cend(),
[](int elem){
return elem > 4 ;
});
std::cout << "number of elements greater than 4: " << num << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
coll:1 2 3 4 5 6 7 8 9
number of elements equal to 4: 1
number of elements with even value: 4
number of elements greater than 4: 5
#endif
#if 0 //algo/minmax1.cpp p526 2021年04月08日 星期四 19时32分41秒
#include <cstdlib>
#include "algostuff.hpp"
bool absLess(int elem1, int elem2){
return abs(elem1) < abs(elem2);
}
int main(){
std::deque<int> coll;
INSERT_ELEMENTS(coll, 2, 6);
INSERT_ELEMENTS(coll, -3, 6);
PRINT_ELEMENTS(coll);
std::cout << "minimum: "
<< *min_element(coll.cbegin(), coll.cend()) << std::endl;
std::cout << "maximum: "
<< *max_element(coll.cbegin(), coll.cend()) << std::endl;
auto mm = minmax_element(coll.cbegin(), coll.cend());
std::cout << "min: " << *(mm.first) << std::endl;
std::cout << "max: " << *(mm.second) << std::endl;
std::cout << "distance: " << distance(mm.first, mm.second) << std::endl;
std::cout << "minimum of absolute values: "
<< *min_element(coll.cbegin(), coll.cend(), absLess) << std::endl;
std::cout << "maximum of absolute values: "
<< *max_element(coll.cbegin(), coll.cend(), absLess) << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
2 3 4 5 6 -3 -2 -1 0 1 2 3 4 5 6
minimum: -3
maximum: 6
min: -3
max: 6
distance: 9
minimum of absolute values: 0
maximum of absolute values: 6
#endif
#if 0 //algo/find1.cpp p528 2021年04月08日 星期四 19时40分56秒
#include "algostuff.hpp"
int main(){
std::list<int> coll;
INSERT_ELEMENTS(coll, 1, 9);
INSERT_ELEMENTS(coll, 1, 9);
PRINT_ELEMENTS(coll, "coll: ");
std::list<int>::iterator pos1;
pos1 = std::find(coll.begin(), coll.end(), 4);
std::list<int>::iterator pos2;
if(pos1 != coll.end()){
pos2 = std::find(++pos1, coll.end(), 4);
}
if(pos1 != coll.end() && pos2 != coll.end()){
std::copy(--pos1, ++pos2,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
coll: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3 4
#endif
#if 0//algo /find2.cpp p529 2021年04月08日 星期四 19时46分39秒
#include "algostuff.hpp"
int main(){
std::vector<int> coll;
std::vector<int>::iterator pos;
INSERT_ELEMENTS(coll, 1, 9);
PRINT_ELEMENTS(coll, "coll: ");
pos = std::find_if(coll.begin(), coll.end(),
std::bind(std::greater<int>(), std::placeholders::_1, 3));
std::cout << "the "
<< std::distance(coll.begin(), pos) + 1
<< ". element is the first greater than 3" << std::endl;
pos = std::find_if(coll.begin(), coll.end(),
[](int elem){
return elem%3 == 0;
});
std::cout << "the "
<< std::distance(coll.begin(), pos) + 1
<< ". element is the first divisible by 3" << std::endl;
std::cout << "the "
<< std::distance(coll.begin(), pos) + 1
<< ". element is the first greater than 3" << std::endl;
pos = std::find_if(coll.begin(), coll.end(),
std::bind(std::less<int>(), std::placeholders::_1, 5));
std::cout << "first value >= 5: " << *pos << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
coll: 1 2 3 4 5 6 7 8 9
the 4. element is the first greater than 3
the 3. element is the first divisible by 3
the 3. element is the first greater than 3
first value >= 5: 1
#endif
#if 0 //algo/searchn1.cpp p531 2021年04月08日 星期四 19时56分02秒
#include "algostuff.hpp"
int main(){
std::deque<int> coll;
coll = {1, 2, 7, 7, 6, 3, 9, 5, 7, 7, 7, 3, 6};
PRINT_ELEMENTS(coll);
std::deque<int>::iterator pos;
pos = search_n(coll.begin(), coll.end(), 3, 7);
if(pos != coll.end()){
std::cout << "three consecutive elements with value 7 "
<< "start with " << std::distance(coll.begin(), pos) + 1
<< ". element" << std::endl;
}else {
std::cout << "no four consecutive elements with value 7 found"
<< std::endl;
}
pos = search_n(coll.begin(), coll.end(), 4, 0,
[](int elem, int value){
return elem % 2 == 1;
});
if(pos != coll.end()){
std::cout << "first four consecutive odd elements are: ";
for(int i = 0; i< 4; ++i, ++pos){
std::cout << *pos << ' ';
}
}else{
std::cout << "no four consecutive elements with value > 3 found";
}
std::cout << std::endl;
return 0;
}wannian07@wannian07-PC:~/Desktop/The Complete Reference$ ./hello
1 2 7 7 6 3 9 5 7 7 7 3 6
three consecutive elements with value 7 start with 9. ele