//Thinking in c++ p 677
//2020年06月19日 10时42分18秒
//PrintSequance.h
//#pragma once
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>
template<typename Iter>
void print(Iter first, Iter last, const char *nm = "",
const char * sep = "\n",
std::ostream & os = std::cout)
{
if(nm != 0 && *nm != '\n')
os << nm << ": " << sep;
typedef typename
std::iterator_traits<Iter>::value_type T;
std::copy(first, last,
std::ostream_iterator<T>(std::cout, sep));
os << std::endl;
}
struct PlusOne{
bool operator()(int i, int j) {return j == i + 1;}
};
class MulMoreThan{
int value;
public:
MulMoreThan(int val) : value(val) { }
bool operator() (int v, int m) { return v * m > value; }
};
#include <cstring>
#include <set>
#include <cstdlib>
class SkipGen{
int i;
int skp;
public:
SkipGen(int start = 0, int skip = 1)
: i(start), skp(skip) {}
int operator()(){
int r = i;
i += skp;
return r;
}
};
class URandGen{
std::set<int> used;
int limit;
public:
URandGen(int lim) : limit(lim){}
int operator()(){
while(true){
int i = int(std::rand()) % limit;
if(used.find(i) == used.end()){
used.insert(i);
return i;
}
}
}
};
class CharGen{
static const char* source;
static const int len;
public:
char operator()(){
return source[std::rand() % len];
}
};
const char* CharGen::source = "ABCDEFGHIJK"
"LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const int CharGen::len = std::strlen(source);
struct IsUpper {
bool operator()(char c) { return isupper(c); }
};
int main(){
int a[] {1, 2, 3, 4, 5, 6, 6, 7, 7, 7, 8,
8, 8, 8, 11, 11, 11, 11, 11};
const int ASZ = sizeof a / sizeof *a;
std::vector<int> v(a, a + ASZ);
print(v.begin(), v.end(), "v", " ");
std::vector<int>::iterator it = find(v.begin(), v.end(), 4);
std::cout << "find: " << *it << std::endl;
it = std::find_if(v.begin(), v.end(),
std::bind2nd(std::greater<int>(), 8));
std::cout << "find_if: " << *it << std::endl;
it = std::adjacent_find(v.begin(), v.end());
while(it != v.end()) {
std::cout << "adjacent_find: " << *it
<< ", " << *(it + 1) << std::endl;
it = adjacent_find(it + 1, v.end());
}
it = adjacent_find(v.begin(), v.end(), PlusOne());
while(it != v.end()) {
std::cout << "adjacent_find PlusOne: " << *it
<< ", " << *(it + 1) << std::endl;
it = adjacent_find(it + 1, v.end(), PlusOne());
}
int b[] {8, 11};
const int BSZ = sizeof b / sizeof *b;
print(b, b + BSZ, "b", " ");
it = std::find_first_of(v.begin(), v.end(), b, b + BSZ);
print(it, it + BSZ, "find_first_of", " ");
it = std::find_first_of(v.begin(), v.end(),
b, b + BSZ, PlusOne());
print(it, it + BSZ, "find_first_of PlusOne", " ");
it = std::search(v.begin(), v.end(), b, b + BSZ);
print(it, it + BSZ, "search", " ");
int c[] {5, 6, 7};
const int CSZ = sizeof c / sizeof *c ;
print(c, c + CSZ, "c", " ");
it = search(v.begin(), v.end(), c, c + CSZ, PlusOne());
print(it, it + CSZ, "search PlusOne", " ");
int d[] {11, 11, 11};
const int DSZ = sizeof d / sizeof *d;
print(d, d + DSZ, "d", " ");
it = std::find_end(v.begin(), v.end(), d, d + DSZ);
print(it, v.end(), "find_end", " " );
int e[] {9, 9};
print(e, e + 2, "e", " ");
it = find_end(v.begin(), v.end(), e, e + 2, PlusOne());
print(it, v.end(), "find_end PlusOne", " ");
it = std::search_n(v.begin(), v.end(), 3, 7);
print(it, it + 3, "search_n 3, 7", " ");
it = search_n(v.begin(), v.end(),
6, 15, MulMoreThan(100));
print(it, it + 6,
"search_n 6, 15, MulMoreThan(100)", " ");
std::cout << "min_element: "
<< *min_element(v.begin(), v.end()) << std::endl;
std::cout << "max_element: "
<< *max_element(v.begin(), v.end()) << std::endl;
std::vector<int> v2;
std::replace_copy(v.begin(), v.end(),
std::back_inserter(v2), 8, 47);
print(v2.begin(), v2.end(), "replace_copy 8 -> 47", " ");
std::replace_if(v.begin(), v.end(),
std::bind2nd(std::greater_equal<int>(), 7), -1);
print(v.begin(), v.end(), "replace_if >= 7 -> -1", " ");
//Thinking in c++ p 681
std::string s1("This is a test");
std::string s2("This is a Test");//唯一区别 T
std::cout << "s1: " << s1 << std::endl << "s2: " << s2 << std::endl;
std::cout << "compare s1 & s1: "
<< std::equal(s1.begin(), s1.end(), s1.begin()) << std::endl;
std::cout << "compare s1 & s2: "
<< std::equal(s1.begin(), s1.end(), s2.begin()) << std::endl;
std::cout << "lexicographical_compare s1 & s1: "
<< std::lexicographical_compare(s1.begin(), s1.end(),
s1.begin(), s1.end()) << std::endl;
std::cout << "lexicographical_compare s1 & s2: "
<< std::lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end()) << std::endl;
//c++ 操作平台, 大写字母字符 "领先于" 小写字母字符
std::cout << "lexicographical_compare s2 & s1: "
<< std::lexicographical_compare(s2.begin(), s2.end(),
s1.begin(), s1.end()) << std::endl;
std::cout << "lexicographical_compare shortened "
"s1 & full-length s2: " << std::endl;
//s1内容复制到s3, 每次循环减去s1末尾的一个字符, 直到测试结果返回true
//实际, s3中依次减到大写T, 之前两序列完全相同, 下面停止了. s3 < s1, s3 "领先于" s1
std::string s3(s1);
while(s3.length() != 0) {
bool result = std::lexicographical_compare(
s3.begin(), s3.end(), s2.begin(),s2.end());
std::cout << s3 << std::endl << s2 << ", result = "
<< result << std::endl;
if(result == true) break;
s3 = s3.substr(0, s3.length() - 1);
}
std::pair<std::string::iterator, std::string::iterator > p =
std::mismatch(s1.begin(), s1.end(), s2.begin());
print(p.first, s1.end(), "p.first", "");
print(p.second, s2.end(), "p.second", "");
std::string v3;
v3.resize(25);
std::generate(v3.begin(), v3.end(), CharGen());
print(v3.begin(), v3.end(), "v3 original", "");
// Create a set of the characters in v:
std::string us(v3.begin(), v3.end());
std::sort(us.begin(), us.end());
std::string::iterator it1 = us.begin(), cit = v3.end(),
uend = unique(us.begin(), us.end());
// Step through and remove everything:
while(it1 != uend) {
cit = std::remove(v3.begin(), cit, *it1);
print(v3.begin(), v3.end(), "Complete v3", "");
print(v3.begin(), cit, "Pseudo v3 ", " ");
std::cout << "Removed element:\t" << *it1
<< "\nPsuedo Last Element:\t"
<< *cit << std::endl << std::endl;
++it1;
}
std::generate(v3.begin(), v3.end(), CharGen());
print(v3.begin(), v3.end(), "v3 ", "");
cit = std::remove_if(v3.begin(), v3.end(), IsUpper());
print(v3.begin(), cit, "v3 after remove_if IsUpper", " ");
// Copying versions are not shown for remove()
// and remove_if().
std::sort(v3.begin(), cit);
print(v3.begin(), cit, "sorted", " ");
std::string v4;
v4.resize(cit - v3.begin());
std::unique_copy(v3.begin(), cit, v4.begin());
print(v4.begin(), v4.end(), "unique_copy", " ");
// Same behavior:
/*
c12.cpp:4114:59: 错误:对‘unique(std::vector<int>::iterator, std::__cxx11::basic_string<char>::iterat or&, std::equal_to<char>)’的调用没有匹配的函数
4114 | cit = std::unique(v.begin(), cit, std::equal_to<char>());
*/
// cit = std::unique(v.begin(), cit, std::equal_to<char>());
// print(v3.begin(), cit, "unique equal_to<char>", " ");
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c12 c12.cpp
wannian07@wannian07-PC:~$ ./c12
v: 1 2 3 4 5 6 6 7 7 7 8 8 8 8 11 11 11 11 11
find: 4
find_if: 11
adjacent_find: 6, 6
adjacent_find: 7, 7
adjacent_find: 7, 7
adjacent_find: 8, 8
adjacent_find: 8, 8
adjacent_find: 8, 8
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find PlusOne: 1, 2
adjacent_find PlusOne: 2, 3
adjacent_find PlusOne: 3, 4
adjacent_find PlusOne: 4, 5
adjacent_find PlusOne: 5, 6
adjacent_find PlusOne: 6, 7
adjacent_find PlusOne: 7, 8
b: 8 11
find_first_of: 8 8
find_first_of PlusOne: 7 7
search: 8 11
c: 5 6 7
search PlusOne: 4 5 6
d: 11 11 11
find_end: 11 11 11
e: 9 9
find_end PlusOne: 8 8 11 11 11 11 11
search_n 3, 7: 7 7 7
search_n 6, 15, MulMoreThan(100): 7 7 7 8 8 8
min_element: 1
max_element: 11
replace_copy 8 -> 47: 1 2 3 4 5 6 6 7 7 7 47 47 47 47 11 11 11 11 11
replace_if >= 7 -> -1: 1 2 3 4 5 6 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
s1: This is a test
s2: This is a Test
compare s1 & s1: 1
compare s1 & s2: 0
lexicographical_compare s1 & s1: 0
lexicographical_compare s1 & s2: 0
lexicographical_compare s2 & s1: 1
lexicographical_compare shortened s1 & full-length s2:
This is a test
This is a Test, result = 0
This is a tes
This is a Test, result = 0
This is a te
This is a Test, result = 0
This is a t
This is a Test, result = 0
This is a
This is a Test, result = 1
p.first: test
p.second: Test
v3 original: nWlrBbmQBhCDarzOwKkYHIDdq
Complete v3: nWlrbmQhCDarzOwKkYHIDdqdq
Pseudo v3 : n W l r b m Q h C D a r z O w K k Y H I D d q
Removed element: B
Psuedo Last Element: d
Complete v3: nWlrbmQhDarzOwKkYHIDdqqdq
Pseudo v3 : n W l r b m Q h D a r z O w K k Y H I D d q
Removed element: C
Psuedo Last Element: q
Complete v3: nWlrbmQharzOwKkYHIdqdqqdq
Pseudo v3 : n W l r b m Q h a r z O w K k Y H I d q
Removed element: D
Psuedo Last Element: d
Complete v3: nWlrbmQharzOwKkYIdqqdqqdq
Pseudo v3 : n W l r b m Q h a r z O w K k Y I d q
Removed element: H
Psuedo Last Element: q
Complete v3: nWlrbmQharzOwKkYdqqqdqqdq
Pseudo v3 : n W l r b m Q h a r z O w K k Y d q
Removed element: I
Psuedo Last Element: q
Complete v3: nWlrbmQharzOwkYdqqqqdqqdq
Pseudo v3 : n W l r b m Q h a r z O w k Y d q
Removed element: K
Psuedo Last Element: q
Complete v3: nWlrbmQharzwkYdqqqqqdqqdq
Pseudo v3 : n W l r b m Q h a r z w k Y d q
Removed element: O
Psuedo Last Element: q
Complete v3: nWlrbmharzwkYdqqqqqqdqqdq
Pseudo v3 : n W l r b m h a r z w k Y d q
Removed element: Q
Psuedo Last Element: q
Complete v3: nlrbmharzwkYdqqqqqqqdqqdq
Pseudo v3 : n l r b m h a r z w k Y d q
Removed element: W
Psuedo Last Element: q
Complete v3: nlrbmharzwkdqqqqqqqqdqqdq
Pseudo v3 : n l r b m h a r z w k d q
Removed element: Y
Psuedo Last Element: q
Complete v3: nlrbmhrzwkdqqqqqqqqqdqqdq
Pseudo v3 : n l r b m h r z w k d q
Removed element: a
Psuedo Last Element: q
Complete v3: nlrmhrzwkdqqqqqqqqqqdqqdq
Pseudo v3 : n l r m h r z w k d q
Removed element: b
Psuedo Last Element: q
Complete v3: nlrmhrzwkqqqqqqqqqqqdqqdq
Pseudo v3 : n l r m h r z w k q
Removed element: d
Psuedo Last Element: q
Complete v3: nlrmrzwkqqqqqqqqqqqqdqqdq
Pseudo v3 : n l r m r z w k q
Removed element: h
Psuedo Last Element: q
Complete v3: nlrmrzwqqqqqqqqqqqqqdqqdq
Pseudo v3 : n l r m r z w q
Removed element: k
Psuedo Last Element: q
Complete v3: nrmrzwqqqqqqqqqqqqqqdqqdq
Pseudo v3 : n r m r z w q
Removed element: l
Psuedo Last Element: q
Complete v3: nrrzwqqqqqqqqqqqqqqqdqqdq
Pseudo v3 : n r r z w q
Removed element: m
Psuedo Last Element: q
Complete v3: rrzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 : r r z w q
Removed element: n
Psuedo Last Element: q
Complete v3: rrzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 : r r z w
Removed element: q
Psuedo Last Element: q
Complete v3: zwzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 : z w
Removed element: r
Psuedo Last Element: z
Complete v3: zwzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 : z
Removed element: w
Psuedo Last Element: w
Complete v3: zwzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :
Removed element: z
Psuedo Last Element: z
v3 : SCDXrJmOWFrxsjyBldbEFSArC
v3 after remove_if IsUpper: r m r x s j y l d b r
sorted: b d j l m r r r s x y
unique_copy: b d j l m r s x y
*/